RSA算法实验报告
现代密码算法实验报告(3篇)

第1篇一、实验目的1. 了解现代密码学的基本原理和数论基础知识;2. 掌握非对称密码体制的著名代表RSA加密算法的工作原理和流程;3. 设计实现一个简单的密钥系统;4. 掌握常用加密算法AES和DES的原理及实现。
二、实验内容1. RSA加密算法实验2. AES加密算法实验3. DES加密算法实验三、实验原理1. RSA加密算法RSA算法是一种非对称加密算法,由罗纳德·李维斯特、阿迪·沙米尔和伦纳德·阿德曼三位密码学家于1977年提出。
其基本原理是选择两个大质数p和q,计算它们的乘积n=pq,并计算欧拉函数φ(n)=(p-1)(q-1)。
选择一个整数e,满足1<e<φ(n)且e与φ(n)互质。
计算e关于φ(n)的模逆元d。
公开密钥为(e,n),私有密钥为(d,n)。
加密过程为C=Me mod n,解密过程为M=Cd mod n。
2. AES加密算法AES(Advanced Encryption Standard)是一种分组加密算法,采用128位分组大小和128、192或256位密钥长度。
AES算法主要分为四个阶段:初始轮、密钥扩展、中间轮和最终轮。
每个轮包括字节替换、行移位、列混淆和轮密钥加。
3. DES加密算法DES(Data Encryption Standard)是一种分组加密算法,采用64位分组大小和56位密钥长度。
DES算法主要分为16轮,每轮包括置换、置换-置换、S盒替换和密钥加。
四、实验步骤及内容1. RSA加密算法实验(1)选择两个大质数p和q,计算n=pq和φ(n)=(p-1)(q-1);(2)选择一个整数e,满足1<e<φ(n)且e与φ(n)互质,计算e关于φ(n)的模逆元d;(3)生成公开密钥(e,n)和私有密钥(d,n);(4)用公钥对明文进行加密,用私钥对密文进行解密。
2. AES加密算法实验(1)选择一个128、192或256位密钥;(2)初始化初始轮密钥;(3)进行16轮加密操作,包括字节替换、行移位、列混淆和轮密钥加;(4)输出加密后的密文。
(完整版)RSA算法实验报告

RSA算法的实现实验原理算法原理RSA公开密钥密码体制。
所谓的公开密钥密码体制就是使用不同的加密密钥与解密密钥,是一种“由已知加密密钥推导出解密密钥在计算上是不可行的”密码体制。
RSA算法是一种非对称密码算法,所谓非对称,就是指该算法需要一对密钥,使用其中一个加密,则需要用另一个才能解密。
RSA的算法涉及三个参数,n、e1、e2。
其中,n是两个大质数p、q的积,n的二进制表示时所占用的位数,就是所谓的密钥长度。
e1和e2是一对相关的值,e1可以任意取,但要求e1与(p-1)*(q-1)互质;再选择e2,要求(e2*e1)mod((p-1)*(q-1))=1。
(n,e1),(n,e2)就是密钥对。
其中(n,e1)为公钥,(n,e2)为私钥。
RSA加解密的算法完全相同,设A为明文,B为密文,则:A=B^e2 mod n;B=A^e1 mod n;(公钥加密体制中,一般用公钥加密,私钥解密)e1和e2可以互换使用,即:A=B^e1 mod n;B=A^e2 mod n;密钥生成首先要使用概率算法来验证随机产生的大的整数是否质数,这样的算法比较快而且可以消除掉大多数非质数。
假如有一个数通过了这个测试的话,那么要使用一个精确的测试来保证它的确是一个质数。
密钥分配和其它加密过程一样,对RSA来说分配公钥的过程是非常重要的。
分配公钥的过程必须能够抵挡一个从中取代的攻击。
假设Eve交给Bob一个公钥,并使Bob相信这是Alice的公钥,并且她可以截下Alice和Bob之间的信息传递,那么她可以将她自己的公钥传给Bob,Bob以为这是Alice的公钥。
步骤如下(这里设B为是实现着)(1)B寻找出两个大素数p和q。
(2)B计算出n=p*q和ϕ(n)=)(p-1)*(q-1)。
(3)B选择一个随机数e(0<e<ϕ(n)),满足(e,ϕ(n))=1 (即e与欧拉函数互素ϕ(n))。
(4)B使用欧几里得算法计算e的模余ϕ(n)的乘法逆元素d。
rsa算法实验报告

rsa算法实验报告RSA算法实验报告摘要:RSA算法是一种非对称加密算法,被广泛应用于网络安全领域。
本实验通过对RSA算法的原理和实现进行了深入研究,并通过编写代码实现了RSA算法的加密和解密过程。
实验结果表明,RSA算法具有较高的安全性和可靠性,能够有效保护数据的机密性和完整性。
一、引言RSA算法是一种基于大数因子分解的非对称加密算法,由Rivest、Shamir和Adleman三位数学家于1977年提出。
它的安全性基于两个大素数的乘积难以分解,因此被广泛应用于数字签名、数据加密等领域。
本实验旨在通过对RSA 算法的原理和实现进行研究,深入了解其加密和解密过程,并通过编写代码实现RSA算法的加密和解密过程。
二、RSA算法原理RSA算法的原理主要包括密钥生成、加密和解密三个过程。
首先,选择两个大素数p和q,并计算它们的乘积n=p*q,然后计算欧拉函数φ(n)=(p-1)*(q-1)。
接下来选择一个整数e,使得1<e<φ(n),且e与φ(n)互质,即e和φ(n)的最大公约数为1。
然后计算e的乘法逆元d,使得(e*d) mod φ(n) = 1。
最后,公钥为(n, e),私钥为(n, d)。
加密过程中,将明文m通过公钥加密为密文c,即c=m^e mod n;解密过程中,将密文c通过私钥解密为明文m,即m=c^d mod n。
三、实验设计本实验使用Python语言编写了RSA算法的加密和解密代码,通过输入明文和密钥,实现了对明文的加密和解密过程。
具体实验步骤如下:1. 选择两个大素数p和q,并计算n=p*q,以及φ(n)=(p-1)*(q-1);2. 选择一个整数e,使得1<e<φ(n),且e与φ(n)互质;3. 计算e的乘法逆元d,使得(e*d) mod φ(n) = 1;4. 将明文m通过公钥加密为密文c,即c=m^e mod n;5. 将密文c通过私钥解密为明文m,即m=c^d mod n。
RSA非对称加密算法实验报告

2009年9月
第一部分
信息加密技术实验
第一部分 信息加密技术实验
一、实验目的:
在数据加密、解密的算法中,DES算法是典型的单密钥体制,RSA是典型的双密 钥体制, 目前均在大量使用。 通过实验, 让学生充分理解和掌握DES和RSA算法,PGP 加密工具的使用。以及通过网络进行数据加密传输的概念。
RSA实验: 运行《RSATool》演示软件: 产生公钥和私钥
第二项任务 DES加密解密工具2.1及其代码——支持字符串及文件加密,支持3重DES
1.DES 2.1 说明
先看看DES 2.1 的截图:
的简介: 2. DES 2.1 2.1的简介:
1.支持任意长度字符串加密解密 2.明文、密钥可以不足8字节 3.支持回车换行,Tab 等特殊字符 4.密文可以选择三种方式显示 5.支持3重DES 6.支持文件加密、解密 7.加密时显示进度
3.DES 算法介绍
关于DES算法的介绍大家可以看我博客里的另一篇文章,[原创]DES算法的介绍以及实
现(含上次DES程序1.0的 源 码 ) ,所以在此不在重述。
提供的 Public 函数 4. yxyDES2 Class Class提供的 提供的Public Public函数
下面我们来看看yxyDES2 类(Class)里public函数和它们的用法: //功能:产生16个28位的key //参数:源8位的字符串(key),存放key的序号0-1 //结果:函数将调用private CreateSubKey将结果存于char SubKeys[keyN][16][48] void InitializeKey(char* srcBytes,unsigned int keyN); //功能:加密8位字符串 //参数:8位字符串,使用Key的序号0-1 //结果:函数将加密后结果存放于private szCiphertext[16] // 用户通过属性Ciphertext得到 void EncryptData(char* _srcBytes,unsigned int keyN); //功能:解密16位十六进制字符串 //参数:16位十六进制字符串,使用Key的序号0-1 //结果:函数将解密候结果存放于private szPlaintext[8] // 用户通过属性Plaintext得到 void DecryptData(char* _srcBytes,unsigned int keyN); //功能:加密任意长度字符串 //参数:任意长度字符串,长度,使用Key的序号0-1 //结果:函数将加密后结果存放于private szFCiphertextAnyLength[8192] // 用户通过属性CiphertextAnyLength 得到 void EncryptAnyLength(char* _srcBytes,unsigned int _bytesLength,unsigned int keyN); //功能:解密任意长度十六进制字符串 //参数:任意长度字符串,长度,使用Key的序号0-1 //结果:函数将加密后结果存放于private szFPlaintextAnyLength[8192] // 用户通过属性PlaintextAnyLength 得到 void DecryptAnyLength(char* _srcBytes,unsigned int _bytesLength, unsigned int keyN); //功能:Bytes到Bits的转换, //参数:待变换字符串,处理后结果存放缓冲区指针,Bits缓冲区大小 void Bytes2Bits(char *srcBytes, char* dstBits, unsigned int sizeBits); //功能:Bits到Bytes的转换, //参数:待变换字符串,处理后结果存放缓冲区指针,Bits缓冲区大小 void Bits2Bytes(char *dstBytes, char* srcBits, unsigned int sizeBits);
RSA算法的实现实验报告

RSA算法的实现实验报告一、实验目的本实验的主要目的是了解和掌握RSA(Rivest-Shamir-Adleman)算法的原理以及其在加密和解密过程中的具体实现。
通过实践和对比分析,了解RSA算法的安全性和效率,并加深对大数计算的理解。
二、算法原理1.密钥生成(1)选择两个大素数p和q,并计算其乘积n=p*q。
(2)计算n的欧拉函数φ(n)=(p-1)*(q-1)。
(3)选择一个整数e,满足1<e<φ(n),并且e与φ(n)互质,即gcd(e, φ(n)) = 1(4)计算e的乘法逆元d,满足e*d ≡ 1 (mod φ(n))。
(5)公钥为(n,e),私钥为(n,d)。
2.加密加密过程使用公钥对明文进行加密:(1)明文转化为整数m,满足0≤m<n。
(2)计算密文c = m^e mod n。
3.解密解密过程使用私钥对密文进行解密:(1)计算明文m = c^d mod n。
(2)还原明文。
三、实验步骤1.实现大数的运算由于RSA算法的关键在于处理大数运算,我们首先实现了大数的加法、减法和乘法运算,并使用这些运算函数作为之后RSA算法中的基础运算。
2.实现RSA算法的密钥生成(1)随机选择两个大素数p和q。
(2)计算n=p*q。
(3)计算φ(n)=(p-1)*(q-1)。
(4)选择一个满足要求的公钥e。
(5)计算e的乘法逆元d。
(6)生成公钥(n,e)和私钥(n,d)。
3.实现RSA算法的加密和解密(1)输入明文。
(2)使用公钥对明文进行加密。
(3)得到密文。
(4)使用私钥对密文进行解密。
(5)还原明文。
四、实验结果与分析我们使用python语言实现了RSA算法,并进行了一些测试和分析,得到以下结果和结论。
1.RSA算法的安全性2.RSA算法的效率3.实验结果分析我们对一些常见文本进行了加密和解密实验,得到了正确的结果。
实验结果表明,RSA算法能够对数据进行有效的加密和解密,并确保数据的安全性。
RSA算法分析和实现

六、教师评语
签名:
日期:
成绩
这个定理说明a经过编码为b在经过解码为C时,a==c mod n(n=pq)
但我们在做编码解码时,限制0<= a < n, 0 <= c < n,
所以这就是说a等於c,所以这个过程确实能做到编码解码的功能。
四、实验结果与数据处理
五、总结
通过对RSA公钥密码体制进行了分析与研究,了解了怎样的密码体制才能充分发挥RSA的安全作用。分析了RSA加密解密的安全性,以及如何选取RSA密钥长度的问题。同时我们考虑到在实际的应用过程中,在满足安全性前提下应当降低计算的复杂度,提高信息加、解密的速度。便于降低成本,利于推广应用等因素,目前国内外对RSA算法实现的研究大多是在运算速度很高的计算机上,在硬件上也主要采用串行处理,为了提高速度,安全性就必然很差,相反,为提高安全强度,则运算处理速度又会降低。在RSA算法中,最基本的算法主要包括模加、模乘、模逆和模幂运算。大数运算很费时间,尤其是大整数的模逆和模幂运算。为了得到较快的加/解密速度,本程序进行了深层次的优化,主要采用移位的方法,大大提高了RSA算法实际应用的运算速度和执行效率。
《应用密码学》实验报告
实验序号:03实验项目名称:RSA密码技术
学 号
姓 名
专业、班
实验地点
指导教师
实验时间
一、实验目的及要求
实验目的:
1.深入理解RSA原理,特别是大数分解难题的原理
2.明确选择RSA的参数p、q、e、d的条件,以及明文分段的条件
3.掌握求逆元素和求高次幂的计算方法
rsa加密 实验报告

rsa加密实验报告RSA加密实验报告概述RSA加密算法是一种非对称加密算法,广泛应用于信息安全领域。
本实验旨在通过实际操作,深入理解RSA加密算法的原理、过程和应用。
实验目的1. 理解RSA加密算法的原理和基本概念;2. 掌握RSA加密算法的加密和解密过程;3. 了解RSA加密算法的应用场景和安全性。
实验材料1. 一台计算机;2. 编程语言或工具,如Python。
实验步骤1. 生成密钥对首先,我们需要生成一对RSA密钥,包括公钥和私钥。
公钥用于加密数据,私钥用于解密数据。
在Python中,可以使用`cryptography`库来生成密钥对。
2. 加密数据选择一段需要加密的数据,可以是文本、图片或其他文件。
将数据使用公钥进行加密,得到密文。
在Python中,可以使用`cryptography`库中的RSA加密函数来实现。
3. 解密数据使用私钥对密文进行解密,还原成原始数据。
在Python中,可以使用`cryptography`库中的RSA解密函数来实现。
4. 实验结果分析分析实验结果,包括加密后的密文和解密后的明文。
观察密文的长度和结构,以及解密过程是否成功。
同时,可以比较不同数据加密的结果,探讨RSA加密算法的安全性和可靠性。
实验注意事项1. 密钥的安全性:私钥是解密数据的关键,必须妥善保管,避免泄露给他人。
公钥可以公开使用,但也需要注意保护,以防止被篡改。
2. 数据大小限制:RSA加密算法对数据的大小有一定限制,一般建议将较大的数据先进行分块处理,然后分别加密和解密。
3. 算法优化:RSA加密算法的性能较低,特别是对大素数的计算。
在实际应用中,可以采用一些优化技术,如使用快速模幂算法,提高加密和解密的效率。
实验结论通过本次实验,我们深入了解了RSA加密算法的原理和过程。
RSA加密算法具有较高的安全性,适用于保护敏感数据的加密和解密。
然而,由于其计算复杂度较高,对于大数据的加密和解密可能存在性能问题。
RSA算法实验报告

RSA算法实验报告第一点:RSA算法原理及其数学基础RSA算法是一种非对称加密算法,于1977年由罗纳德·李维斯特(Ron Rivest)、阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)提出。
它的名称就是这三位发明者姓氏的首字母缩写。
RSA算法的出现,为信息安全领域带来了重大的变革,它不仅解决了密钥的分发问题,还提供了加密和解密功能。
RSA算法的核心是基于整数分解的难解性。
假设我们有一个大整数N,它是由两个大质数p和q相乘得到的,即N=pq。
我们知道,分解N为p和q是非常困难的,尤其是在N非常大的情况下。
这就是RSA算法的安全性所在。
RSA算法的步骤如下:1.选择两个大的质数p和q,计算N=pq,再计算欧拉函数φ(N)=(p-1)(q-1)。
2.选择一个与φ(N)互质的整数e,计算d,使得ed≡1(mod φ(N))。
3.将(N,e)作为公钥,(N,d)作为私钥。
4.加密:明文M转换为0到N-1之间的整数m,密文c≡m^e(mod N)。
5.解密:密文c转换为0到N-1之间的整数c,明文m≡c^d(mod N)。
第二点:RSA算法的实现与分析在实际应用中,RSA算法的实现主要包括以下几个步骤:1.随机选择两个大的质数p和q。
为了确保N的安全性,通常需要选择几千位的质数。
2.计算N=pq和φ(N)=(p-1)(q-1)。
3.选择一个与φ(N)互质的整数e,通常选择65537,因为它是一个质数,并且在模运算中具有较好的性能。
4.计算d,使得ed≡1(mod φ(N))。
5.输出公钥(N,e)和私钥(N,d)。
RSA算法的分析主要关注以下几个方面:1.安全性:RSA算法的安全性主要取决于N的质数因子p和q的大小。
当N的位数足够多时,分解N为p和q是非常困难的。
2.性能:RSA算法的加密和解密速度较慢,尤其是当N的位数较多时。
因此,RSA算法更适合用于加密较小的数据,如密钥交换和数字签名。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验二非对称密码算法RSA一、实验目的通过实际编程了解非对称密码算法RSA勺加密和解密过程,加深对非对称密码算法的认识。
二、实验环境运行Windows或Linux操作系统的PC机,具有版本的Java语言编译环境。
三、实验内容和步骤1、对RSA算法的理解RSA算法(公开密钥算法)的原理:(1) •选择两个大的素数p和q (典型情况下为1024位)(2) .计算n = p * q 和z = ( p-1 ) * ( q-1 ) .(3) .选择一个与z 互素的数,将它称为d⑷. 找到e,使其满足e*d = 1 mod z提前计算出这些参数以后,我们就可以开始执行加密了。
首先将明文分成块,使得每个明文消息P落在间隔0*P<n中。
为了做到这一点,只要将明文划分成k 位的块即可,这里k是满足2A k<n的最大整数。
为了加密一个消息P,只要计算C=P A e(mod n)即可。
为了解密C,只要计算P=C A d(modn)即可。
可以证明,对于指定范围内的所有P,加密盒解密互为反函数。
为了执行加密,你需要e和n;为了执行解密,你需要d和n。
因此,公钥是有(e,n)对组成,而私钥是有(d,n)对组成。
实例:根据已知参数:p=3,q=11,M=2计算公私钥,并对明文进行加密,然后对密文进行解密。
由题意知:n = p * q = 33, z = (p-1 ) * (q-1 )= 20,选 d = 7,计算得e=3,所以C=MAe(mod n) = 8M=CAd(mod n) = 22、RSA 算法与DES 算法的比较:运行附件的RSATool,输入一大段文字,记录运行时间。
再使用DES算法加密相同的文字,记录运行时间,对比这两个时间发现,RSA算法比DES算法慢很多,因为RSA算法进行的是大数运算,所以程序运行的速度比DES慢很多。
因此RSA算法只适合于少量数据加密,不适合于大量数据加密。
3、算法设计主要的方法:(1)、public static void GetPrime()方法名称:产生大数的方法。
说明:利用Java语言的中的类的方法中随机产生大数。
(2)、public static boolean MillerRobin(BigInteger num) 方法名称:判断是否是素数的方法。
参数说明:num 是由GetPrime 方法产生的大数。
说明:这个方法判断GetPrime 方法传过来的是否是一个素数,是就返回true, 否就返回false。
3)、public static BigInteger powmod( BigInteger a, BigInteger t, 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 为私钥。
4、源程序:(文件) import .*;import import .*;import .*;import .*;import .*; public class RSA1{public static void main(String[] args) {MyFrame frame = new MyFrame();MyPanel_fbutton panel_fbutton = new MyPanel_fbutton(frame,,,,; FlowLayout fl = newFlowLayout ,0,0);(fl); (panel_fbutton);( 150, 100, 500, 480 );J(true);}}class 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);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);{{}class MyPanel_fbutton extends JPanel{public MyPanel_fbutton(Frame aframe,JTextField aP, JTextField aQ, JTextField 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;}class MyPanel_p extends JPanel{public MyPanel_p(JTextField aP){P=aP;add(new JLabel(" 质数 P:"));add(P);}private JTextField P;}class MyPanel_q extends JPanel{public MyPanel_q(JTextField aQ){Q=aQ;add(new JLabel(" 质数 Q:"));add(Q);}private JTextField Q;}class MyPanel_d extends JPanelpublic MyPanel_d(JTextField ad)d=ad;add(new JLabel("公 钥 :")); ad,{{add(d);}private JTextField d;}class MyPanel_e extends JPanel{public MyPanel_e(JTextField ae){e=ae;add(new JLabel(" 私钥:"));add(e);}private JTextField e;}class 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=;int h=;}class 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=;int h=;}class MyPanel_out1 extends JPanelpublic MyPanel_out1(JTextArea aoutput1)output1 = aoutput1;add(new JLabel(" 解密后的明文:"));JScrollPane jsp = new JScrollPane (output1,v,h); add(jsp);}private JTextArea output1;int v=;int h=;}class 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 ;(new RandListener( P, Q )); add(randProduce);(new RandDListener( P, Q, d, e )); add(randD);(new EncodeListener( P, Q, d, e, input, output )); add(encode);(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(" 解密");}class 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);(true);String infileName =()+();String inStr = new String();inStr = (infileName);BigInteger PrimeP = new BigInteger());BigInteger PrimeQ = new BigInteger());BigInteger n =(PrimeQ);int nLen = ();int m=(int)((double)(nLen)/);nLen = (nLen-1) / 16;String outStr = new String();outStr = (inStr,PrimeP,PrimeQ,n,nLen,m,D); for(i=()-1;i>=0;--i){if(i)=='.') break;}String outfileName = (0,i);outfileName = outfileName + new String(".EncodeRsa") + (i,()); (outfileName,outStr);}private JTextField P;private JTextField 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(ActionEvent ee){FileDialog fd = new FileDialog(fr);(true);String infileName =()+();String inStr = new String(); inStr = (infileName);BigInteger PrimeP = new BigInteger());BigInteger PrimeQ = new BigInteger()); BigInteger n =(PrimeQ); int nLen = ();int m=(int)((double)(nLen)/); nLen = (nLen-1) / 16; String outStr = new String(); outStr =(inStr,PrimeP,PrimeQ,n,nLen,m,E); for(i=()-1;i>=0;--i){ if(i)=='.') break;}String outfileName = (0,i); outfileName = outfileName + new String(".DecodeRsa") +(i,()); (outfileName,outStr);}private JTextField P;private JTextField Q;private JTextField E;private JTextField D;private Frame fr;int i;}class RandListener implements ActionListener{public RandListener(JTextField aP, JTextField aQ) { P = aP;Q = aQ;}public void actionPerformed(ActionEvent e) {( P );( Q );}private JTextField P;private JTextField Q;}class 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());BigInteger QQ = new BigInteger());BigInteger temp = (new BigInteger("1"))).multiply(new BigInteger("1")));BigInteger temp1;do{temp1=new BigInteger(100, new Random()).mod(temp);}while(temp1)==false);());(temp1, temp).toString());}private JTextField P;private JTextField Q;private JTextField d;private 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 = new BigInteger());BigInteger PrimeQ = new BigInteger());BigInteger n =(PrimeQ);int nLen = ();int m=(int)((double)(nLen)/);nLen = (nLen-1) / 16;String inStr = (); (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;}class 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());BigInteger PrimeQ = new BigInteger());BigInteger n =(PrimeQ);int nLen = ();int m=(int)((double)(nLen)/);nLen = (nLen-1) / 16;String inStr = (); (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)()*20+100);num = new BigInteger( length, 5 , rand );());} while(MillerRobin(num)==false);}public static boolean MillerRobin(BigInteger num){int time = 1000;BigInteger mod = (new BigInteger("2"));if(new BigInteger("0"))){return false;}int s = 0, j=0;BigInteger t=(new BigInteger("1"));while( (new BigInteger("2")).equals("0") ){(new BigInteger("2"));++s;}for(int i=0; i<time; ++i){BigInteger a = new BigInteger(100, newRandom()).mod(new BigInteger("3"))).add(new BigInteger("2"));BigInteger y = powmod(a, t, num); if(new BigInteger("1"))==false&& (new BigInteger("1")))==false){j=1;while(j==s&&(new BigInteger("1")))==false){y = (y).mod(num);if(new BigInteger("1"))){return false;}++j;}if(new BigInteger("1")))==false){return false;}}}return true;}public static BigInteger powmod( BigInteger a, BigInteger t, BigInteger num ) {BigInteger A = new BigInteger("1");while(new BigInteger("0"))==false){if(new BigInteger("2")).equals(new BigInteger("1"))){A = (a).mod(num);}a = (a).mod(num);t=(new BigInteger("2"));}return A;}public static BigInteger invmod(BigInteger a, BigInteger b){" "+b);BigInteger s0=new BigInteger("1"), s1=new BigInteger("0"), s2, q, t, b0=b; while(new BigInteger("0"))==false){q=(b);s2=( (s1) );if(new BigInteger("0"))!=-1){s2=(b0);}else{s2=( (new BigInteger("-1")).mod(b0) );}s0=s1;s1=s2;t=b;b=(b);a=t;}if(new BigInteger("1"))){return s0;}else{return new BigInteger("0");}}public static String Encode(String inStr,BigInteger PrimeP,BigInteger PrimeQ, BigInteger n,intnLen,int m,JTextField d){BigInteger res = new BigInteger("0");StringBuffer outBuf = new StringBuffer();int i,k,j;for(i=0;i<();i=j){BigInteger t = new BigInteger("0");for(j=i;j<i+nLen&&j<();j++){t=(16);long num = (j);t=(num));}res = (t,new BigInteger()),n);String buf = new String();for(k=0;k<m;++k){long num = (65535))).longValue(); res = (16);buf = (char)(num)+buf;} outBuf = (buf);}return ();}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<();i+=j){BigInteger t = new BigInteger("0"); for(j=0;j<m&&j+i<();j++) {t = (16);long num =(long) (j+i)); t=(num));}res = (t,new BigInteger()),n);String buf = new String(); while(new BigInteger("0"))>0) {long num =(65535))).longV alue(); buf = (char)(num) + buf; res = (16) ;}outBuf = (buf);}return ();}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= (); if(t==-1) break; ans = ans + (char)(t);}();();();}catch(FileNotFoundException e){"FileStreamsTest: "+e);} catch(IOException e){"FileStreamsTest: "+e);}"READSTR="+());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<();i++){c=(int)(i);(c);}();();();}catch(FileNotFoundException e){"FileStreamsTest: "+e);}catch(IOException e)"FileStreamsTest: "+e);}"WRITE="+());return ;}public static String input(String infileName){String ans = new String();try{FileInputStream in = new FileInputStream(infileName); ObjectInputStream s = newObjectInputStream(in); ans = (String)();();();}catch(FileNotFoundException e){{"FileStreamsTest: "+e);} catch(IOException e){"FileStreamsTest: "+e);}catch(ClassNotFoundException e){"FileStreamsTest: "+e);}return ans;}public static void output(String outfileName,String outStr){try{FileOutputStream f = new FileOutputStream(outfileName); ObjectOutputStream s =new ObjectOutputStream(f); (outStr);();();}catch(FileNotFoundException e){"FileStreamsTest: "+e);}catch(IOExcepti on e){"FileStreamsTest: "+e);}}}四、实验结果运行程序,弹出的对话框如上图所示。