import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import javax.crypto.Cipher;
public class RSA0414 extends WindowAdapter implements ActionListener,ItemListener{
String puk=null;//公钥
String bts=null;//加密后的数据
String prk=null;//私钥
String btt=null;//解密后的数据
Label l1=newLabel("输入:"),l2=newLabel("加密:"),l3=newLabel("解密:"),l4=newLabel("私钥:"),l5=newLabel("公钥:");
JFrame f;
TextField t1=newTextField(60),t2=newTextField(60),t3=newTextField(60);
TextArea t4=newTextArea(" ",5,60,TextArea.SCROLLBARS_BOTH),t5=newTextArea(" ",5,60,TextArea.SCROLLBARS_BOTH);
Button b1=newButton("加密"),b2=newButton("解密"),b3=newButton("清屏");
Choice c1;
Panel p1,p2,p3,p4,p5,p6,p7,p8,p9;
public void display(){
f=newJFrame("中国矿业大学密码学课程设计 学号:08093739 欧二强 ");
f.setSize(540,400);
f.setLocation(200,140);
f.setBackground(Color.lightGray);
f.setLayout(new BorderLayout());
tiajiazujian();
f.setVisible(true);
}
/*添加界面组件以及监听器*/
public void tiajiazujian(){
c1=newChoice();
c1.add("实现公钥密码算法RSA");
c1.addItemListener(this);
f.add(c1,"North");
p1=newPanel();
p3=newPanel();
f.add(p3,"Center");
p3.setLayout(new FlowLayout());
p3.add(l1);p3.add(t1);p3.add(l2);p3.add(t2);p3.add(l3);p3.add(t3);
p2=newPanel();
f.add(p2,"South");
//p2.setLayout(new FlowLayout());//GridLayout(2,2));
p4=newPanel();
//f.add(p4,"Center");
p3.add(l4);p3.add(t4);p2.add(b1);p2.add(b2);p2.add(b3);p3.add(l5);p3.add(t5);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
}
/*对应不同加密算法的按钮点击事件*/
public void actionPerformed(ActionEvent e){
if (e.getSource()==b3&&c1.getSelectedIndex()==0){
t1.setText(" ");t2.setText(" ");t3.setText(" ");
}
if (e.getSource()==b1&&c1.getSelectedIndex()==0)
{
//ywjiami();
rsaJiami();
}
if(e.getSource()==b2&&c1.getSelectedIndex()==0){
//ywjiemi();
rsaJiemi();
}
}
public void itemStateChanged(ItemEvent e){
}
/*实现公钥密码算法RSA**/
//RSA加密算法
public void rsaJiami(){
try {
RSAEncrypt encrypt=newRSAEncrypt();
//Scanner read=newScanner(System.in);
//System.out.println("请输入您要加密的数据:");
String s1;
String s2;
s1=t1.getText();//输入解密数据
String encryptText=s1;
KeyPairGenerator keyPairGen=KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(1024);
KeyPair keyPair=keyPairGen.generateKeyPair();
RSAPrivateKey privateKey= (RSAPrivateKey) keyPair.getPrivate();
RSAPublicKey publicKey= (RSAPublicKey) keyPair.getPublic();
//System.out.println("请输入您要加密的数据:");
byte[] e=encrypt.encrypt(publicKey, encryptText.getBytes());
byte[] de=encrypt.decrypt(privateKey,e);
//System.out.println("加密的数据:");
//System.out.println(encrypt.bytesToString(e));
//System.out.println("解密的数据:");
//System.out.println(encrypt.bytesToString(de));
//System.out.println("公钥:"+publicKey);
//System.out.println("密钥:"+privateKey);
//puk=publicKey.toString();//公钥
bts=encrypt.bytesToString(e);//加密后的数据
//prk=privateKey.toString();//私钥
//btt=encrypt.bytesToString(de);//解密后的数据
t2.setText(bts);//加密结果
t3.setText(btt);//解密结果
//t4.replaceRange(prk,0,10);
//t5.replaceRange(puk,0,10);
} catch (Exception e) {
e.printStackTrace();
}
}
//RSA解密算法
public void rsaJiemi(){
try {
RSAEncrypt encrypt=newRSAEncrypt();
//Scanner read=newScanner(System.in);
//System.out.println("请输入您要加密的数据:");
String s1;
String s2;
s1=t1.getText();//输入解密数据
String encryptText=s1;
KeyPairGenerator keyPairGen=KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(1024);
KeyPair keyPair=keyPairGen.generateKeyPair();
RSAPrivateKey privateKey= (RSAPrivateKey) keyPair.getPrivate();
RSAPublicKey publicKey= (RSAPublicKey) keyPair.getPublic();
//System.out.println("请输入您要加密的数据:");
byte[] e=encrypt.encrypt(publicKey, encryptText.getBytes());
byte[] de=encrypt.decrypt(privateKey,e);
//System.out.println("加密的数据:");
//System.out.println(encrypt.bytesToString(e));
//System.out.println("解密的数据:");
//System.out.println(encrypt.bytesToString(de));
//System.out.println("公钥:"+publicKey);
//System.out.println("密钥:"+privateKey);
puk=publicKey.toString();//公钥
//bts=encrypt.bytesToString(e);//加密后的数据
prk=privateKey.toString();//私钥
btt=encrypt.bytesToString(de);//解密后的数据
//t2.setText(bts);//加密结果
t3.setText(btt);//解密结果
t4.replaceRange(prk,0,10);
t5.replaceRange(puk,0,10);
} catch (Exception e) {
e.printStackTrace();
}
}
//主方法
public static void main(String arg[])
{
RSA0414 ob=newRSA0414();
ob.display();
}
}
这个Java程序演示了如何使用RSA公钥密码算法进行加密和解密操作。用户可以输入数据,程序会生成RSA密钥对,用公钥加密数据,并用私钥解密数据。

867

被折叠的 条评论
为什么被折叠?



