rsa加解密 java 用图形用户界面
时间: 2023-08-01 12:14:17 浏览: 59
为了实现RSA加解密的图形用户界面,需要使用Java图形界面工具包(Java Swing)来创建窗口和组件。以下是一个简单的RSA加解密GUI示例:
```java
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Security;
import javax.crypto.Cipher;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class RSAGUI implements ActionListener {
private JFrame frame;
private JTextField publicKeyField;
private JTextField privateKeyField;
private JTextArea inputArea;
private JTextArea outputArea;
private JButton encryptButton;
private JButton decryptButton;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
RSAGUI window = new RSAGUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public RSAGUI() {
initialize();
}
private void initialize() {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
frame = new JFrame();
frame.setBounds(100, 100, 600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.CENTER);
panel.setLayout(null);
JLabel publicKeyLabel = new JLabel("Public Key:");
publicKeyLabel.setBounds(10, 10, 80, 20);
panel.add(publicKeyLabel);
publicKeyField = new JTextField();
publicKeyField.setBounds(100, 10, 400, 20);
publicKeyField.setEditable(false);
panel.add(publicKeyField);
JLabel privateKeyLabel = new JLabel("Private Key:");
privateKeyLabel.setBounds(10, 40, 80, 20);
panel.add(privateKeyLabel);
privateKeyField = new JTextField();
privateKeyField.setBounds(100, 40, 400, 20);
privateKeyField.setEditable(false);
panel.add(privateKeyField);
JLabel inputLabel = new JLabel("Input:");
inputLabel.setBounds(10, 70, 80, 20);
panel.add(inputLabel);
inputArea = new JTextArea();
inputArea.setBounds(100, 70, 400, 100);
panel.add(inputArea);
JLabel outputLabel = new JLabel("Output:");
outputLabel.setBounds(10, 190, 80, 20);
panel.add(outputLabel);
outputArea = new JTextArea();
outputArea.setBounds(100, 190, 400, 100);
outputArea.setEditable(false);
panel.add(outputArea);
encryptButton = new JButton("Encrypt");
encryptButton.setBounds(100, 310, 100, 30);
encryptButton.addActionListener(this);
panel.add(encryptButton);
decryptButton = new JButton("Decrypt");
decryptButton.setBounds(220, 310, 100, 30);
decryptButton.addActionListener(this);
panel.add(decryptButton);
try {
// 生成RSA密钥对
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "BC");
generator.initialize(2048);
KeyPair keyPair = generator.generateKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();
publicKeyField.setText(publicKey.toString());
privateKeyField.setText(privateKey.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == encryptButton) {
// 使用公钥加密数据
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC");
cipher.init(Cipher.ENCRYPT_MODE, publicKeyField.getText());
byte[] input = inputArea.getText().getBytes();
byte[] cipherText = cipher.doFinal(input);
outputArea.setText(new String(cipherText));
} else if (e.getSource() == decryptButton) {
// 使用私钥解密数据
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC");
cipher.init(Cipher.DECRYPT_MODE, privateKeyField.getText());
byte[] cipherText = inputArea.getText().getBytes();
byte[] plainText = cipher.doFinal(cipherText);
outputArea.setText(new String(plainText));
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
```
在上面的示例中,通过调用`initialize()`方法创建了一个包含公钥、私钥、输入、输出和加密、解密按钮的窗口。单击加密按钮将使用公钥加密输入区域中的文本,并将结果显示在输出区域中。单击解密按钮将使用私钥解密输入区域中的文本,并将结果显示在输出区域中。需要注意的是,上面的示例中使用的是Bouncy Castle作为Java加密扩展提供者,需要将Bouncy Castle库添加到项目中才能正常运行。