1、实验二 非对称密码算法RSA一、实验目的通过实际编程了解非对称密码算法RSA的加密和解密过程,加深对非对称密码算法的认识。二、实验环境运行Windows或Linux操作系统的PC机,具有JDK1.6版本的Java语言编译环境。三、实验内容和步骤1. 对RSA算法的理解RSA算法(公开密钥算法)的原理:(1) 选择两个大的素数p和q(典型情况下为1024位)(2) 计算n = p * q 和 z =(p-1)*(q-1).(3) 选择一个与z互素的数,将它称为d(4) 找到e,使其满足e*d = 1 mod z提前计算出这些参数以后,我们就可以开始执行加密了。首先将明文分成块,使得每个明文消息P
2、落在间隔0*Pn中。为了做到这一点,只要将明文划分成k位的块即可,这里k是满足2k=0;-i) if(infileName.charAt(i)=.) break; String outfileName = infileName.substring(0,i); outfileName = outfileName + new String(.EncodeRsa) + infileName.substring(i,infileName.length(); PublicMethod.output(outfileName,outStr); private JTextField P; private JT
3、extField Q; private JTextField E; private JTextField D; private Frame fr; int i; class FileDecodeListener implements ActionListener public FileDecodeListener(Frame f,JTextField ap, JTextField aq,JTextField ae,JTextField ad) P = ap; Q = aq; E = ae; D = ad; fr = f; public void actionPerformed(ActionEv
4、ent ee) FileDialog fd = new FileDialog(fr); fd.setVisible(true); String infileName =fd.getDirectory()+fd.getFile(); String inStr = new String(); inStr = PublicMethod.input(infileName); System.out.println(inStr); BigInteger PrimeP = new BigInteger(P.getText(); BigInteger PrimeQ = new BigInteger(Q.get
5、Text(); BigInteger n =PrimeP.multiply(PrimeQ); int nLen = n.bitLength(); int m=(int)(Math.ceil(double)(nLen)/16.0); nLen = (nLen-1) / 16; String outStr = new String(); outStr = PublicMethod.Decode(inStr,PrimeP,PrimeQ,n,nLen,m,E); for(i=infileName.length()-1;i=0;-i) if(infileName.charAt(i)=.) break;
6、String outfileName = infileName.substring(0,i); outfileName = outfileName + new String(.DecodeRsa) + infileName.substring(i,infileName.length(); PublicMethod.write(outfileName,outStr); private JTextField P; private JTextField Q; private JTextField E; private JTextField D; private Frame fr; int i; cl
7、ass RandListener implements ActionListener public RandListener(JTextField aP, JTextField aQ) P = aP; Q = aQ; public void actionPerformed(ActionEvent e) PublicMethod.GetPrime( P ); PublicMethod.GetPrime( Q ); private JTextField P; private JTextField Q; class RandDListener implements ActionListener pu
8、blic RandDListener(JTextField aP, JTextField aQ, JTextField ad, JTextField ae) P = aP; Q = aQ; d = ad; e = ae; public void actionPerformed(ActionEvent ee) BigInteger PP = new BigInteger(P.getText(); BigInteger QQ = new BigInteger(Q.getText(); BigInteger temp = (PP.subtract(new BigInteger(1).multiply
9、(QQ.subtract(new BigInteger(1); BigInteger temp1; do temp1=new BigInteger(100, new Random().mod(temp); while(PublicMethod.MillerRobin(temp1)=false); d.setText(temp1.toString(); e.setText(PublicMethod.invmod(temp1, temp).toString(); private JTextField P; private JTextField Q; private JTextField d; pr
10、ivate JTextField e; class EncodeListener implements ActionListener public EncodeListener(JTextField aP, JTextField aQ, JTextField ad, JTextField ae, JTextArea in, JTextArea out) P = aP; Q = aQ; d = ad; e = ae; input = in; output = out; public void actionPerformed(ActionEvent ee) BigInteger PrimeP =
11、new BigInteger(P.getText(); BigInteger PrimeQ = new BigInteger(Q.getText(); BigInteger n =PrimeP.multiply(PrimeQ); int nLen = n.bitLength(); int m=(int)(Math.ceil(double)(nLen)/16.0); nLen = (nLen-1) / 16; String inStr = input.getText(); output.setText(PublicMethod.Encode(inStr,PrimeP,PrimeQ,n,nLen,
12、m,e); private JTextField P; private JTextField Q; private JTextField d; private JTextField e; private JTextArea input; private JTextArea output; class DecodeListener implements ActionListener public DecodeListener(JTextField aP, JTextField aQ, JTextField ad, JTextField ae, JTextArea out, JTextArea o
13、ut1) P = aP; Q = aQ; d = ad; e = ae; output = out; output1 = out1; public void actionPerformed(ActionEvent ee) BigInteger PrimeP = new BigInteger(P.getText(); BigInteger PrimeQ = new BigInteger(Q.getText(); BigInteger n =PrimeP.multiply(PrimeQ); int nLen = n.bitLength(); int m=(int)(Math.ceil(double
14、)(nLen)/16.0); nLen = (nLen-1) / 16; String inStr = output.getText(); output1.setText(PublicMethod.Decode(inStr,PrimeP,PrimeQ,n,nLen,m,d); private JTextField P; private JTextField Q; private JTextField d; private JTextField e; private JTextArea output; private JTextArea output1; class PublicMethod public static void GetPrime( JTextField prime ) BigInteger num = new BigInteger(0); Random rand = new Random(); do int length = (int)(Math.random()*20+100); System.out.println(length); num = new BigInteger( length, 5 , rand ); prime.setText(num.toString(); while(MillerRobin(num)=false); pu