RSA实验报告_rsa实验报告
RSA实验报告由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“rsa实验报告”。
一 实验目的1.了解非对称加密机制 2.理解RSA算法的加解密原理
3.熟悉Java的学习以及运用Java实现RSA算法的加解密过程
二 实验背景
在公开密钥密码体制中,加密密钥(即公开密钥)PK是公开信息,而解密密钥(即秘密密钥)SK是需要保密的。加密算法E和解密算法D也都是公开的。虽然秘密密钥SK是由公开密钥PK决定的,但却不能根据PK计算出SK。正是基于这种理论,1978年出现了著名的RSA算法,它通常是先生成一对RSA 密钥,其中之一是保密密钥,由用户保存;另一个为公开密钥,可对外公开,甚至可在网络服务器中注册。为提高保密强度,RSA密钥至少为500位长,一般推荐使用1024位。这就使加密的计算量很大。为减少计算量,在传送信息时,常采用传统加密方法与公开密钥加密方法相结合的方式,即信息采用改进的DES或IDEA对话密钥加密,然后使用RSA密钥加密对话密钥和信息摘要。对方收到信息后,用不同的密钥解密并可核对信息摘要。
RSA算法是第一个能同时用于加密和数字签名的算法,也易于理解和操作。RSA是被研究得最广泛的公钥算法,从提出到现在的这么多年里,经历了各种攻击的考验,逐渐为人们接受,普遍认为是目前最优秀的公钥方案之一。
三 实验原理
1.非对称密钥加解密概述
使用对称密钥加密体制进行保密通信时,任意不同的两个用户之间都应该使用互不相同的密钥。这样,如果一个网络中有n个用户,他们之间彼此都可能进行秘密通信,这时网络中将需要n(n-1)/2个密钥(其中,每个用户都需要保存n-1个密钥),这样巨大的密钥量给密钥分配和管理带来了极大的困难。另外,随着计算机网络,特别是因特网的发展,网络上互不相识的用户可能需要进行保密的会话(例如,如果用户在进行电子商务活动时,需要保密的连接,这时的客户对象可能根本不是固定的对象)。最后,对称密钥加密机制难以解决签名验证问题。
非对称密钥加密也称为公开密钥加密,或者叫做公钥加密算法。使用公开密钥密码的每一个用户都分别拥有两个密钥:加密密钥和解密密钥,它们两者并不相同,并且由加密密钥得到解密密钥在计算机上是不可行的。每一个用户的加密密钥都是公开的。因此,加密密钥也称为公开密钥。所有用户的公开密钥都将记录在作用类似于电话号码薄的密钥本上,而它可以被所有用户访问,这样每一个用户都可以得到其他所有用户的公开密钥。同时,每一个用户的解密密钥将由用户保存并严格保密。因此,解密密钥也称为私有密钥。
非对称密码算法解决了对称密码体制中密钥管理的难题,并提供了对信息发送人的身份进行验证的手段,是现代密码学最重要的发明。公钥加密算法一般是将对密钥的求解转化为对数学上的困难问题的求解,例如RSA算法的安全性是建立在“大数分解和素性检测”这个数论难题的基础上,已知两个大素数a和b,求出a*b是容易计算的,而已知a*b,想知道其是哪两个大素数的乘积目前还没有好的计算方法,另外也有一些非对称加密算法(如ELGamal算法)的安全性是基于求“离散对数”这个数学难题上的。
在公钥密码系统中每个实体都有自己的公钥和相应的私钥。公钥密码系统的加密变换和解密变换分别用E和D表示。任何实体B要向实体A发送信息m的步骤如下:实体B首先获得实体A的真实公钥的拷贝(eA),实体B使用eA计算密文c=E(m)并发送给实体A,实体A使用自己的私钥dA,计算m=D(c)解密密文,恢复出明文m。这里公钥不需要保密,但要保证它的真实性,即eA确实是实体A掌握的私钥dA所对应的公钥。提供真实的公钥比安全地分配密钥实现起来要容易得多。这也是公钥密码系统的主要优点之一。
公钥密码系统的主要目的是提供保密性,它不能提供数据源认证(data origin authentication)和数据完整性(data integrity)。数据源认证是指:指定的数据是在以前的某个时间确实是由真正的源创建的。数据完整性是指:真正的源创建该数据后经过传输后存储没有发生改变。数据源认证和数据完整性要由其他技术来提供(如消息认证码技术、数字签名技术等)。
从本质上来看,公钥密码比对称密钥密码加密的速度要慢,粗略的说,公钥加密算法RSA硬件实现比分组加密算法DES硬件实现的速度慢1500倍,而软件实现的速度要慢100倍。
公钥解密也可以提供认证保证(如:在实体认证协议、带认证的密钥建立协议等)。公钥加密中必须有颁发让发送消息的人得到想要发送到的那个人的公钥的真实拷贝,否则就会受到伪装攻击。在实践中有很多方法分发真实的公钥,如:使用可信的公共文件,使用在线可信服务器,使用离线服务器和认证。
2.公钥加解密的优缺点:
1)大型网络中的每个用户需要的密钥数量少。
2)对管理公钥的可信第三方的信任程度要求不高而且是离线的。3)只有私钥是保密的,而公钥只要保证它的真实性。4)多数公钥加密比对称密钥加密的速度要慢几个数量级。5)公钥加密方案的密钥长度比对称加密的密钥要长。6)公钥加密方案没有被证明是安全的。
公钥密码的概念本身就被公认为是密码学上的一块里程碑。三十多年来的研究表明,公钥密码成功地解决了计算机网络安全中的密钥管理,身份认证和数字签名等问题,已经成为信息安全技术中的重大核心技术。
四 RSA算法
1.概述
RSA加密算法于1977年由美国麻省理工学院的Ronal Rivest,Adi Shamir和Len Adleman三位年轻教授提出,并以三人的姓氏Rivest,Shamir和Adleman命名为RSA算法。这三位科学家荣获2002年度图灵奖,以表彰他们在算法方面的突出贡献。该算法利用了数论领域的一个事实,那就是虽然把两个大质数相乘生成一个合数是件十分容易的事情,但要把一个合数分解为两个质数的乘积却十分困难。合数分解问题目前仍然是数学领域尚未解决的一大难题,至今没有任何高效的分解方法。它无须收发双方同时参与加密过程,既可以用于保密也可以用于签名,因而非常适合于电子邮件系统的加密,互连网和信用卡安全系统。
算法概述:找两素数p和q,取n=p*q,取t=(p-1)*(q-1),取任何一个数e,要求满足e
2.算法设计
1)public static void GetPrime()说明:利用Java语言的中的java.math.BigInteger类的方法中随机产生大数。2)public static boolean MillerRobin(BigInteger num)参数说明:num是由GetPrime方法产生的大数。
说明:这个方法判断GetPrime方法传过来的是否是一个素数,是就返回true,否就返回false。
3)public static BigInteger powmod(BigIntegera,BigIntegert,BigInteger num)说明:这个方法对传入的大数进行幂运算。
4)public static BigInteger invmod(BigInteger a,BigInteger b)说明:这个方法对大数进行取模运算。
5)public static String Encode(String inStr,BigInteger PrimeP,BigInteger PrimeQ,BigInteger n,int nLen,int m,JTextField d)方法名称:加密算法。
参数说明:
inStr是从界面输入的明文。
PrimeP和PrimeQ是由GetPrime方法产生的两个大素数。n是由PrimeP和PrimeQ得到的值。nLen为n的长度。
d为公钥。
6)public static String Decode(String inStr,BigInteger PrimeP,BigInteger PrimeQ,BigInteger n,int nLen,int m,JTextField e)方法名称:解密算法。参数说明:
inStr是从界面输入的明文。
PrimeP和PrimeQ是由GetPrime方法产生的两个大素数。n是由PrimeP和PrimeQ得到的值。nLen为n的长度。e为私钥。
在对称加密中:n,d两个数构成公钥,可以告诉别人;n,e两个数构成私钥,e自己保留,不让任何人知道。给别人发送的信息使用e加密,只要别人能用d解开就证明信息是由你发送的,构成了签名机制。别人给你发送信息时使用d加密,这样只有拥有e的你能够对其解密。
RSA的安全性在于对于一个大数n,没有有效的方法能够将其分解从而在已知n,d的情况下无法获得e;同样在已知n,e的情况下无法求得d。五 实验源代码
import javax.swing.*;
import java.awt.event.*;
import java.math.*;
import java.util.*;
import java.awt.*;
import java.io.*;
public cla RSA1{
public static void main(String[] args)
{
MyFrame frame = new MyFrame();
MyPanel_fbutton panel_fbutton MyPanel_fbutton(frame,frame.P,frame.Q,frame.d,frame.e);
FlowLayout fl = new FlowLayout(FlowLayout.CENTER,0,0);
frame.setLayout(fl);
frame.add(panel_fbutton);
frame.setBounds(150, 100, 500, 480);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
cla MyFrame extends JFrame
{
public MyFrame()
{
setTitle(“RSA算法”);
add(wel);
MyPanel_p panel_p = new MyPanel_p(P);
add(panel_p);
MyPanel_q panel_q = new MyPanel_q(Q);
add(panel_q);
MyPanel_d panel_d = new MyPanel_d(d);
add(panel_d);
MyPanel_e panel_e = new MyPanel_e(e);
add(panel_e);
MyPanel_in panel_in = new MyPanel_in(input);
add(panel_in);
MyPanel_out panel_out = new MyPanel_out(output);
add(panel_out);
=
new
MyPanel_out1 panel_out1 = new MyPanel_out1(output1);
add(panel_out1);
MyPanel_button panel_button = new MyPanel_button(P,Q,d,e,input,output,output1);
add(panel_button);
}
private JLabel wel = new JLabel(“
RSA算法演示
”);
protected JTextField P = new JTextField(35);
protected JTextField Q = new JTextField(35);
protected JTextField d = new JTextField(35);
protected JTextField e = new JTextField(35);
protected JTextArea input = new JTextArea(4,35);
protected JTextArea output = new JTextArea(4,35);
protected JTextArea output1 = new JTextArea(4,35);
}
cla MyPanel_fbutton extends JPanel
{
public MyPanel_fbutton(Frame aframe,JTextField aP, JTextField aQ, JTextField ad, JTextField ae)
{
frame = aframe;
P = aP;
Q = aQ;
e = ae;
d = ad;
}
private Frame frame;
private JTextField P;
private JTextField Q;
private JTextField d;
private JTextField e;
}
cla MyPanel_p extends JPanel
{
public MyPanel_p(JTextField aP)
{
P=aP;
add(new JLabel(“
质数 P:”));
add(P);
}
private JTextField P;
}
cla MyPanel_q extends JPanel
{
public MyPanel_q(JTextField aQ)
{
Q=aQ;
add(new JLabel(“
质数 Q:”));
add(Q);
}
private JTextField Q;
}
cla MyPanel_d extends JPanel
{
public MyPanel_d(JTextField ad)
{
d=ad;
add(new JLabel(“
公
钥:”));
add(d);
}
private JTextField d;
}
cla MyPanel_e extends JPanel
{
public MyPanel_e(JTextField ae)
{
e=ae;
add(new JLabel(“
私
钥:”));
add(e);
}
private JTextField e;
}
cla MyPanel_in extends JPanel
{
public MyPanel_in(JTextArea ainput)
{
input = ainput;
add(new JLabel(“
输入明文:”));
JScrollPane jsp1 = new JScrollPane(input,v,h);
add(jsp1);
}
private JTextArea input;
int v=JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED;
int h=JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED;
}
cla MyPanel_out extends JPanel
{
public MyPanel_out(JTextArea aoutput)
{
output = aoutput;
add(new JLabel(“
生成的密文:”));
JScrollPane jsp = new JScrollPane(output,v,h);
add(jsp);
}
private JTextArea output;
int v=JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED;
int h=JScrollPane.HORIZONTAL_SCROLLBAR_NEVER;
}
cla MyPanel_out1 extends JPanel
{
public MyPanel_out1(JTextArea aoutput1)
{
output1 = aoutput1;
add(new JLabel(“解密后的明文:”));
JScrollPane jsp = new JScrollPane(output1,v,h);
add(jsp);
}
private JTextArea output1;
int v=JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED;
int h=JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED;
}
cla MyPanel_button extends JPanel
{
public MyPanel_button(JTextField aP,JTextField aQ,JTextField ad,JTextField ae,JTextArea ainput,JTextArea aoutput,JTextArea aoutput1)
{
P = aP;
Q = aQ;
e = ae;
d = ad;
input = ainput;
output = aoutput;
output1 = aoutput1;
randProduce.addActionListener(new RandListener(P, Q));
add(randProduce);
randD.addActionListener(new RandDListener(P, Q, d, e));
add(randD);
encode.addActionListener(new EncodeListener(P, Q, d, e, input, output));
add(encode);
decode.addActionListener(new DecodeListener(P, Q, d, e, output, output1));
add(decode);
}
private JTextField P;
private JTextField Q;
private JTextField d;
private JTextField e;
private JTextArea input;
private JTextArea output;
private JTextArea output1;
private JButton randProduce = new JButton(“生成质数P和Q”);
private JButton randD = new JButton(“生成公钥和私钥”);
private JButton encode = new JButton(“加密”);
private JButton decode = new JButton(“解密”);
}
cla FileEncodeListener implements ActionListener
{
public FileEncodeListener(Frame f,JTextField ap, JTextField aq,JTextField ae,JTextField ad)
{
P = ap;
Q = aq;
E = ae;
D = ad;
fr = f;
}
public void actionPerformed(ActionEvent ee)
{
FileDialog fd = new FileDialog(fr);
fd.setVisible(true);
String infileName =fd.getDirectory()+fd.getFile();
String inStr = new String();
inStr = PublicMethod.read(infileName);
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)(nLen)/16.0));
nLen =(nLen-1)/ 16;
String outStr = new String();
outStr = PublicMethod.Encode(inStr,PrimeP,PrimeQ,n,nLen,m,D);
for(i=infileName.length()-1;i>=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 JTextField Q;
private JTextField E;
private JTextField D;
private Frame fr;
int i;
}
cla 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(ActionEvent 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.getText());
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;
}
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;
}
cla 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;
}
cla RandDListener implements ActionListener
{
public 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(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;
private JTextField e;
}
cla 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 = 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,m,e));
}
private JTextField P;
private JTextField Q;
private JTextField d;
private JTextField e;
private JTextArea input;
private JTextArea output;
}
cla DecodeListener implements ActionListener
{
public DecodeListener(JTextField aP, JTextField aQ, JTextField ad, JTextField ae,JTextArea out, JTextArea out1)
{
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)(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;
}
cla 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);
}
public static boolean MillerRobin(BigInteger num)
{
int time = 1000;
BigInteger mod = num.mod(new BigInteger(“2”));
if(mod.equals(new BigInteger(“0”)))
{
return false;
}
int s = 0, j=0;
BigInteger t=num.subtract(new BigInteger(“1”));
while(t.mod(new BigInteger(“2”)).equals(“0”))
{
t.divide(new BigInteger(“2”));
++s;
}
for(int i=0;i
{
BigInteger a = new BigInteger(100, new Random()).mod(num.subtract(new BigInteger(“3”))).add(new BigInteger(“2”));
BigInteger y = powmod(a, t, num);
if(y.equals(new BigInteger(“1”))==false
&& y.equals(num.subtract(new BigInteger(“1”)))==false)
{
j=1;
while(j==s&&y.equals(num.subtract(new BigInteger(“1”)))==false)
{
y = y.multiply(y).mod(num);
if(y.equals(new BigInteger(“1”)))
{
return false;
}
++j;
}
if(y.equals(num.subtract(new BigInteger(“1”)))==false)
{
return false;
}
}
}
return true;
}
public static BigInteger powmod(BigInteger a, BigInteger t, BigInteger num)
{
BigInteger A = new BigInteger(“1”);
while(t.equals(new BigInteger(“0”))==false)
{
if(t.mod(new BigInteger(“2”)).equals(new BigInteger(“1”)))
{
A = A.multiply(a).mod(num);
}
a = a.multiply(a).mod(num);
t=t.divide(new BigInteger(“2”));
}
return A;
}
public static BigInteger invmod(BigInteger a, BigInteger b)
{
System.out.println(a+“ ”+b);
BigInteger s0=new BigInteger(“1”), s1=new BigInteger(“0”), s2, q, t, b0=b;
while(b.equals(new BigInteger(“0”))==false)
{
q=a.divide(b);
s2=s0.subtract(q.multiply(s1));
if(s2.compareTo(new BigInteger(“0”))!=-1)
{
s2=s2.mod(b0);
}
else
{
s2=b0.subtract(s2.multiply(new BigInteger(“-1”)).mod(b0));
}
s0=s1;
s1=s2;
t=b;
b=a.mod(b);
a=t;
}
if(a.equals(new BigInteger(“1”)))
{
return s0;
}
else
{
return new BigInteger(“0”);
}
}
public static String Encode(String inStr,BigInteger PrimeP,BigInteger PrimeQ,BigInteger n,int nLen,int m,JTextField d)
{
BigInteger res = new BigInteger(“0”);
StringBuffer outBuf = new StringBuffer();
int i,k,j;
for(i=0;i
{
BigInteger t = new BigInteger(“0”);
for(j=i;j
{
t=t.shiftLeft(16);
long num = inStr.charAt(j);
t=t.add(BigInteger.valueOf(num));
}
res = PublicMethod.powmod(t,new BigInteger(d.getText()),n);
String buf = new String();
for(k=0;k
{
long num =(res.and(BigInteger.valueOf(65535))).longValue();
res = res.shiftRight(16);
buf =(char)(num)+buf;
}
outBuf = outBuf.append(buf);
}
return outBuf.toString();
}
public static String Decode(String inStr,BigInteger PrimeP,BigInteger PrimeQ,BigInteger n,int nLen,int m,JTextField e)
{
StringBuffer outBuf = new StringBuffer();
BigInteger res = new BigInteger(“0”);
int i,j;
for(i=0;i
{
BigInteger t = new BigInteger(“0”);
for(j=0;j
{
t = t.shiftLeft(16);
long num =(long)(inStr.charAt(j+i));
t=t.add(BigInteger.valueOf(num));
}
res = PublicMethod.powmod(t,new BigInteger(e.getText()),n);
String buf = new String();
while(res.compareTo(new BigInteger(“0”))>0)
{
long num =(res.and(BigInteger.valueOf(65535))).longValue();
buf =(char)(num)+ buf;
res = res.shiftRight(16)
;
}
outBuf = outBuf.append(buf);
}
return outBuf.toString();
}
public static String read(String infileName)
{
String ans = new String();
try
{
FileInputStream fis = new FileInputStream(infileName);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
int t;
while(true)
{
t= br.read();
System.out.println(t);
if(t==-1)break;
ans = ans +(char)(t);
}
br.close();
isr.close();
fis.close();
}
catch(FileNotFoundException e)
{
System.out.println(“FileStreamsTest: ”+e);
}
catch(IOException e)
{
System.err.println(“FileStreamsTest: ”+e);
}
System.out.println(“READSTR=”+ans.length());
return ans;
}
public static void write(String outfileName,String outStr)
{
try
{
FileOutputStream fos = new FileOutputStream(outfileName);
OutputStreamWriter osw = new OutputStreamWriter(fos,“UNICODE”);
BufferedWriter out = new BufferedWriter(osw);
int c;
for(int i=0;i
{
c=(int)outStr.charAt(i);
System.out.println(c);
out.write(c);
}
out.close();
osw.close();
fos.close();
}
catch(FileNotFoundException e)
{
System.out.println(“FileStreamsTest: ”+e);
}
catch(IOException e)
{
System.err.println(“FileStreamsTest: ”+e);
}
System.out.println(“WRITE=”+outStr.length());
return;
}
public static String input(String infileName)
{
String ans = new String();
try
{
FileInputStream in = new FileInputStream(infileName);
ObjectInputStream s = new ObjectInputStream(in);
ans =(String)s.readObject();
s.close();
in.close();
}
catch(FileNotFoundException e)
{
System.out.println(“FileStreamsTest: ”+e);
}
catch(IOException e)
{
System.err.println(“FileStreamsTest: ”+e);
}
catch(ClaNotFoundException e)
{
System.out.println(“FileStreamsTest: ”+e);
}
return ans;
}
public static void output(String outfileName,String outStr)
{
try
{
FileOutputStream f = new FileOutputStream(outfileName);
ObjectOutputStream s = new ObjectOutputStream(f);
s.writeObject(outStr);
s.close();
f.close();
}
catch(FileNotFoundException e)
{
System.out.println(“FileStreamsTest: ”+e);
}
catch(IOException e)
{
System.err.println(“FileStreamsTest: ”+e);
}
} }
六 实验结果及分析
实验结果如图6-1所示
图6-1 运行结果界面
运行程序,弹出的对话框如上图所示。
点击按钮“生成质数P和Q”,自动生成质数P和质数Q; 然后点击按钮“生成公钥和私钥”,就自动生成公钥和私钥; 输入明文:密码学有点意思
点击“加密”按钮,生成的密文如上图所示。点击“解密”按钮,即可解密密文。如图。
图6-2 Java运行界面
七 实验小结
本次实验对输入的任意一段明文字母,实现了输出对应密钥的密文字母。亲手实际编写RSA密码算法代码,更好的了解和掌握了RSA的相关内容。通过用Java对RSA密码体制进行编程,对RSA密码体制的加解密过程有了更深入的理解。通过这个实验更是让我获得了很多实际工作中所要具备的能力。