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库添加到项目中才能正常运行。

相关推荐

最新推荐

recommend-type

Java实现的RSA加密解密算法示例

主要介绍了Java实现的RSA加密解密算法,结合实例形式分析了java RAS加密解密算法的相关实现技巧,需要的朋友可以参考下
recommend-type

C# RSA分段加解密实现方法详解

主要介绍了C# RSA分段加解密实现方法,结合具体实例形式分析了C# RSA加密解密的原理与具体实现技巧,需要的朋友可以参考下
recommend-type

C++实现密码学 RSA加密解密算法

RSA加密解密算法 C++ 密码学 RSA加密解密算法 C++ 密码学 RSA加密解密算法 C++ 密码学
recommend-type

SQLSERVER加密解密函数(非对称密钥 证书加密 对称密钥)使用方法代码

主要介绍了SQLSERVER加密解密函数使用方法,使用了非对称密钥、证书加密、对称密钥、通行短语(PassPhrase)加密,大家参考使用吧
recommend-type

rsa加密解密算法C语言代码

rsa加密解密算法C语言代码 #include #include #include <stdlib.h> #include <time.h> #include #include #define MAX 100 #define LEN sizeof(struct slink) void sub(int a[MAX],int b[MAX] ,int c[MAX] ); ...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MySQL数据库性能提升秘籍:揭秘性能下降幕后真凶及解决策略

![MySQL数据库性能提升秘籍:揭秘性能下降幕后真凶及解决策略](http://xiaoyuge.work/explain-sql/index/2.png) # 1. MySQL数据库性能优化概述** MySQL数据库性能优化是一项至关重要的任务,可以显著提高应用程序的响应时间和整体用户体验。优化涉及识别和解决影响数据库性能的因素,包括硬件资源瓶颈、软件配置不当和数据库设计缺陷。通过采取适当的优化策略,可以显著提升数据库性能,满足业务需求并提高用户满意度。 # 2. MySQL数据库性能下降的幕后真凶 ### 2.1 硬件资源瓶颈 #### 2.1.1 CPU利用率过高 **症状:
recommend-type

如何在unity创建按钮

在 Unity 中创建按钮的步骤如下: 1. 在 Unity 中创建一个 UI Canvas,选择 GameObject -> UI -> Canvas。 2. 在 Canvas 中创建一个按钮,选择 GameObject -> UI -> Button。 3. 在场景视图中调整按钮的位置和大小。 4. 在 Inspector 中设置按钮的文本、颜色、字体等属性。 5. 添加按钮的响应事件,选择按钮,在 Inspector 的 On Click () 中添加相应的方法。 这样就可以创建一个按钮了,你可以在游戏中使用它来触发相应的操作。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。