2
我有一個Java程序。這是一個AES加密 - 解密程序。該程序有一個圖形用戶界面,可以輸入字符串並顯示AES加密字符串。該程序還使用代碼中寫入的解密函數顯示原始字符串。界面是這樣的,當輸入字符串並且點擊轉換按鈕時,加密和解密結果顯示在面板中。這是程序。AES加密 - 如何以後解密加密的字符串?
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;
public class AESGUI extends JPanel {
public static void main(String[] args) {
JFrame frame = new JFrame("AES Encryption");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(600,300));
frame.setLocationRelativeTo(null);
frame.setResizable(false);
AESGUI p = new AESGUI();
frame.getContentPane().add(p);
frame.pack();
frame.setVisible(true);
}
private JTextField in;
private JTextArea out;
public AESGUI() {
JLabel info = new JLabel("Type any String");
in = new JTextField(20);
JButton encrypt = new JButton("Encrypt");
out = new JTextArea(10,40);
out.setEditable(false);
encrypt.addActionListener(new encryptListener());
in.addActionListener(new encryptListener());
add(info);
add(in);
add(encrypt);
add(out);
add(new JScrollPane(out));
}
private class encryptListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String data = in.getText();
if (data.length() == 0) { }
else
try {
String en = encrypt(data);
out.append("Encrypted string: " + en + "\n");
out.append("Original String: " + decrypt(en) + "\n\n");
} catch(Exception ex) { }
}
}
public String asHex(byte[] buf) {
StringBuffer strbuf = new StringBuffer(buf.length * 2);
int i;
for (i = 0; i < buf.length; i++) {
if (((int) buf[i] & 0xff) < 0x10)
strbuf.append("0");
strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
}
return strbuf.toString();
}
private SecretKeySpec skeySpec;
private Cipher cipher;
private byte[] encrypted;
public String encrypt(String str) throws Exception {
// Get the KeyGenerator
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128); // 192 and 256 bits may not be available
// Generate the secret key specs.
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
skeySpec = new SecretKeySpec(raw, "AES");
// Instantiate the cipher
cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
encrypted = cipher.doFinal(str.getBytes());
return asHex(encrypted);
}
public String decrypt(String str) throws Exception {
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] original = cipher.doFinal(encrypted);
String originalString = new String(original);
return originalString;
}
}
該程序不告訴用戶哪個密鑰用於加密字符串。因爲它沒有告訴用戶密鑰,用戶以後不能解密加密的字符串。該程序以十六進制編碼顯示加密的字符串。爲了稍後對字符串進行解密,最好是更改程序以便程序根據密碼創建一個密鑰或者更好地將程序改變爲向用戶顯示隨機生成的密鑰後來解密字符串?
程序的目的是什麼?你想達到什麼目的? – ewernli 2009-12-16 09:00:32