在eclipse中用Java编写AES和RSA混合加密的交互界面
时间: 2024-02-03 19:15:50 浏览: 34
以下是一个简单的Java Swing界面,用于演示如何使用AES和RSA混合加密。
首先,请确保您已经安装了Java SDK和Eclipse IDE。接下来,按照以下步骤操作:
1. 在Eclipse中创建一个新的Java项目。
2. 在项目中创建一个名为“aesrsa”的包。
3. 在“aesrsa”包中创建一个名为“Main.java”的类,该类将包含主方法和Swing用户界面。
以下是完整的代码:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.crypto.*;
import java.security.*;
public class Main {
private JFrame frame;
private JLabel messageLabel;
private JTextField messageField;
private JLabel aesLabel;
private JTextField aesKeyField;
private JLabel rsaLabel;
private JTextField rsaKeyField;
private JButton encryptButton;
private JButton decryptButton;
private JTextArea outputArea;
private Cipher aesCipher;
private Cipher rsaCipher;
public Main() {
initUI();
initCiphers();
}
private void initUI() {
frame = new JFrame("AES and RSA Hybrid Encryption");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 400);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 2));
messageLabel = new JLabel("Message:");
messageField = new JTextField();
aesLabel = new JLabel("AES Key:");
aesKeyField = new JTextField();
rsaLabel = new JLabel("RSA Key:");
rsaKeyField = new JTextField();
panel.add(messageLabel);
panel.add(messageField);
panel.add(aesLabel);
panel.add(aesKeyField);
panel.add(rsaLabel);
panel.add(rsaKeyField);
encryptButton = new JButton("Encrypt");
encryptButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
encrypt();
}
});
decryptButton = new JButton("Decrypt");
decryptButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
decrypt();
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(encryptButton);
buttonPanel.add(decryptButton);
outputArea = new JTextArea();
outputArea.setEditable(false);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(panel, BorderLayout.NORTH);
contentPane.add(buttonPanel, BorderLayout.CENTER);
contentPane.add(outputArea, BorderLayout.SOUTH);
frame.setVisible(true);
}
private void initCiphers() {
try {
aesCipher = Cipher.getInstance("AES");
KeyGenerator aesKeyGen = KeyGenerator.getInstance("AES");
SecureRandom random = new SecureRandom();
aesKeyGen.init(random);
SecretKey aesKey = aesKeyGen.generateKey();
aesKeyField.setText(bytesToHex(aesKey.getEncoded()));
rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
KeyPairGenerator rsaKeyGen = KeyPairGenerator.getInstance("RSA");
rsaKeyGen.initialize(1024, random);
KeyPair rsaKeyPair = rsaKeyGen.generateKeyPair();
rsaKeyField.setText(bytesToHex(rsaKeyPair.getPublic().getEncoded()));
} catch (Exception e) {
e.printStackTrace();
}
}
private void encrypt() {
try {
String message = messageField.getText();
byte[] aesKeyBytes = hexToBytes(aesKeyField.getText());
SecretKeySpec aesKeySpec = new SecretKeySpec(aesKeyBytes, "AES");
aesCipher.init(Cipher.ENCRYPT_MODE, aesKeySpec);
byte[] encryptedMessage = aesCipher.doFinal(message.getBytes());
byte[] rsaKeyBytes = hexToBytes(rsaKeyField.getText());
X509EncodedKeySpec rsaKeySpec = new X509EncodedKeySpec(rsaKeyBytes);
KeyFactory rsaKeyFactory = KeyFactory.getInstance("RSA");
PublicKey rsaPublicKey = rsaKeyFactory.generatePublic(rsaKeySpec);
rsaCipher.init(Cipher.ENCRYPT_MODE, rsaPublicKey);
byte[] encryptedAesKey = rsaCipher.doFinal(aesKeyBytes);
outputArea.setText("Encrypted message:\n" + bytesToHex(encryptedMessage) + "\n\nEncrypted AES key:\n" + bytesToHex(encryptedAesKey));
} catch (Exception e) {
e.printStackTrace();
}
}
private void decrypt() {
try {
byte[] encryptedMessage = hexToBytes(outputArea.getText().split("\n")[1]);
byte[] encryptedAesKey = hexToBytes(outputArea.getText().split("\n")[4]);
byte[] rsaKeyBytes = hexToBytes(rsaKeyField.getText());
PKCS8EncodedKeySpec rsaKeySpec = new PKCS8EncodedKeySpec(rsaKeyBytes);
KeyFactory rsaKeyFactory = KeyFactory.getInstance("RSA");
PrivateKey rsaPrivateKey = rsaKeyFactory.generatePrivate(rsaKeySpec);
rsaCipher.init(Cipher.DECRYPT_MODE, rsaPrivateKey);
byte[] aesKeyBytes = rsaCipher.doFinal(encryptedAesKey);
SecretKeySpec aesKeySpec = new SecretKeySpec(aesKeyBytes, "AES");
aesCipher.init(Cipher.DECRYPT_MODE, aesKeySpec);
byte[] decryptedMessage = aesCipher.doFinal(encryptedMessage);
outputArea.append("\n\nDecrypted message:\n" + new String(decryptedMessage));
} catch (Exception e) {
e.printStackTrace();
}
}
private static String bytesToHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("%02X", b));
}
return sb.toString();
}
private static byte[] hexToBytes(String hex) {
int len = hex.length();
byte[] bytes = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
bytes[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + Character.digit(hex.charAt(i+1), 16));
}
return bytes;
}
public static void main(String[] args) {
new Main();
}
}
```
在代码中,我们使用了Java的加密API来实现AES和RSA混合加密。我们还使用了Java的Swing库来创建用户界面。
在用户界面中,我们有三个文本字段:消息、AES密钥和RSA密钥。我们还有两个按钮:“加密”和“解密”。当用户单击“加密”按钮时,我们将使用AES加密消息,并使用RSA加密AES密钥。当用户单击“解密”按钮时,我们将解密AES密钥,然后使用AES解密消息。
注意,在代码中,我们使用了一些辅助方法来将字节数组转换为十六进制字符串,以及将十六进制字符串转换回字节数组。
您可以将此代码复制到Eclipse中,并运行它以查看它的工作原理。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)