2009-12-16 132 views
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; 
    } 

} 

该程序不告诉用户哪个密钥用于加密字符串。因为它没有告诉用户密钥,用户以后不能解密加密的字符串。该程序以十六进制编码显示加密的字符串。为了稍后对字符串进行解密,最好是更改程序以便程序根据密码创建一个密钥或者更好地将程序改变为向用户显示随机生成的密钥后来解密字符串?

+0

程序的目的是什么?你想达到什么目的? – ewernli 2009-12-16 09:00:32

回答

1

如果用户是在晚些时候使用加密密钥解密加密数据的实际最终用户,我没有看到为用户显示密钥的任何错误。

您也可以使用第一个选项从密码生成加密密钥,但在这种情况下,如果您希望用户稍后解密数据,则需要再次输入加密密码并生成加密密钥(也要确保它能产生密钥)并且使用户感到亲密。