RSA算法实验报告

合集下载

(完整版)RSA算法实验报告

(完整版)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。

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

现代密码算法实验报告(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算法是一种非对称加密算法,被广泛应用于网络安全领域。

本实验通过对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算法实现报告

RSA算法实现报告
• • • • • • • • 1.选择两个不同的大素数p、q (目前两个数的长度都接近512bit是安全的); 2. 计算n = p*q。 3. 计算n的欧拉函数 t=(p-1)(q-1)。 4. 选择整数e作为公钥,使e与t互素,且1<e< t。 5. 用欧几里得算法计算d作为私钥,使d*e=1 mod t。 6. 加密:C=M^e mod n 7. 解密:M=C^d mod n=(M^e)^d mod n= M^e^d mod n 。
RSA算法实现报告
小组成员:王嘉钰、陈梁柱、赖娜、伍沛云

• • • •
• • •


1. RSA算法介绍与应用现 2. 算法原理 3. RSA算法数论基础 4. RSA算法的各环节 4.1.RSA公钥加密解密概述 4.2. RSA签名算法 5. RSA的安全性 6. 代码实现: 7. RSA算法结果分析 7.1.主界面初始化 7.2.设置密钥 7.3.对明文加密 7.4.对密文解密 8. 总结与展望
4.2 RSA签名算法
• 并非所有的公开密钥系统,均可同时达到秘密性与数字签名功能。一 般而言, 一公开密钥系统若作为密码系统,则无法作为数字签名,反 之亦然。只有很少数的系统可同时作为密码系统和数字签名,如本文 讨论的RSA系统。RSA签名算法如下: 设N=p q,且p和q是两个大素数,e和d满足e d≡l(mod ≯(N))。 公开密钥:N,e 私有密钥:d 签名过程:发送方使用自己的私钥d对明文m进行数字签名变换: y=x d mod N:并将加密后的消息和签名y发送给接收方; 验证过程:接收方使用发送方的公钥e对收到的消息y进行数字签名验 证变换x’=ye mod N,并使用发送方的密钥解密恢复消息x,比较x’与x, 如果x’=x则证实发送方的身份合法。

rsa加密算法测试的用例及测试结果

rsa加密算法测试的用例及测试结果

RSA加密算法测试用例及测试结果1. 介绍RSA(Rivest-Shamir-Adleman)是一种非对称加密算法,被广泛应用于信息安全领域。

它的安全性基于大数分解的困难性,即将一个大数因数分解为两个质数的乘积。

在本文中,我们将编写用例来测试RSA加密算法,并给出测试结果。

2. 测试目标我们的测试目标是验证RSA加密算法在不同场景下的正确性和性能。

具体来说,我们将测试以下几个方面: - 密钥生成:验证生成的公钥和私钥是否正确且符合预期。

- 加密解密:验证加密后能否成功解密,且解密结果与原始明文一致。

- 签名验签:验证使用私钥签名后能否使用公钥成功验签,且验签结果正确。

- 性能:测试RSA算法在不同输入规模下的执行时间。

3. 测试用例设计3.1 密钥生成3.1.1 生成长度为1024位的RSA密钥对输入:密钥长度为1024位输出:公钥和私钥3.1.2 生成长度为2048位的RSA密钥对输入:密钥长度为2048位输出:公钥和私钥3.2 加密解密3.2.1 使用1024位RSA公钥加密输入:明文、1024位RSA公钥输出:密文3.2.2 使用1024位RSA私钥解密输入:密文、1024位RSA私钥输出:明文3.2.3 使用2048位RSA公钥加密输入:明文、2048位RSA公钥输出:密文3.2.4 使用2048位RSA私钥解密输入:密文、2048位RSA私钥输出:明文3.3 签名验签3.3.1 使用1024位RSA私钥对数据签名输入:数据、1024位RSA私钥输出:签名3.3.2 使用1024位RSA公钥对签名进行验签输入:数据、签名、1024位RSA公钥输出:验签结果(True/False)3.3.3 使用2048位RSA私钥对数据签名**输入:* 数据、2048位RSA私钥输出:签名3.3.4 使用2048位RSA公钥对签名进行验签输入: 数据、签名、2048位RSA公钥输出: 验签结果(True/False)3.4 性能测试3.4.1 加密解密性能测试输入:不同长度的明文、RSA密钥对输出:加密解密时间3.4.2 签名验签性能测试输入:不同长度的数据、RSA密钥对输出:签名验签时间4. 测试结果4.1 密钥生成测试用例输入输出结果测试用例输入输出结果生成长度为1024位的RSA密钥对密钥长度为1024位公钥和私钥通过生成长度为2048位的RSA密钥对密钥长度为2048位公钥和私钥通过4.2 加密解密测试用例输入输出结果使用1024位RSA公钥加密明文、1024位RSA公钥密文通过使用1024位RSA私钥解密密文、1024位RSA私钥明文通过使用2048位RSA公钥加密明文、2048位RSA公钥密文通过使用2048位RSA私钥解密密文、2048位RSA私钥明文通过4.3 签名验签测试用例输入输出结果使用1024位RSA私钥对数据签名数据、1024位RSA私钥签名通过使用1024位RSA公钥对签名进行验签数据、签名、1024位RSA公钥验签结果(True/False)通过使用2048位RSA私钥对数据签名数据、2048位RSA私钥签名通过使用2048位RSA公钥对签名进行验签数据、签名、2048位RSA公钥验签结果(True/False)通过4.4 性能测试4.4.1 加密解密性能测试在不同长度的明文和密钥对下,记录加密和解密的时间,并绘制折线图。

实验3_RSA加解密算法

实验3_RSA加解密算法

实验3:RSA加密、解密算法实验一、实验目的掌握数论的重要概念和算法(模逆算法、大整数幂的模运算)。

掌握RSA算法。

二、RSA算法原理:1.生成密钥参数①任取两个比较大的素数p,q②计算整数n,n=p*q③计算欧拉函数φ(n)=(p-1)*(q-1)④计算一个较小的正整数w,w应该满足条件gcd(w,φ(n))=1⑤计算一个正整数 d,d应该满足条件w d≡1 (mod φ(n))注:参数(w,n)用于加密,参数(d,n)用于解密2. 加密算法假设需要加密的明文是整数m,加密后的密文是c3.解密算法三、实验任务【实验任务一】(必做)小素数RSA算法①.从键盘输入两个素数p,q,并输出参数n,φ(n),w,d②.从键盘输入明文数据m,计算出密文数据c,输出c③.根据计算机出的密文数据c,计算出明文数据m,输出m④从键盘输入明文字符串mStr,例如"I love you",或者"我爱你"都可以,计算出密文数据cStr,输出cStr⑤解密计算,根据密文数据cStr,计算出明文数据mStr【实验任务二】(可选)大素数RSA算法①给定一个比较整数下限xmin(比如要求xmin>100000),xmin可以大于现有的整数表示范围,因此要求用大整数表示。

②自动计算选取两个大于xmin的大素数p,q,并输出p,q③从键盘输入明文字符串mStr,例如"I love you",或者"我爱你"都可以,计算出密文数据cStr,输出cStr④解密计算,根据密文数据cStr,计算出明文数据mStr。

rsa加密算法测试的用例及测试结果

rsa加密算法测试的用例及测试结果

rsa加密算法测试的用例及测试结果RSA加密算法是一种非对称加密算法,通常用于加密和解密大量数据。

在进行RSA算法的测试时,需要针对不同情况设计用例,测试算法的加密性能、解密正确性、加解密效率等关键性能指标,以确保算法的可靠性和安全性。

下面是一些常见的RSA加密算法测试用例及测试结果:1.生成密钥对测试用例:a.输入一个素数p和另一个素数q,测试生成密钥对的过程。

b.输入一个非素数p或q,测试生成密钥对的过程。

测试结果:对于素数p和q,生成密钥对的过程应该成功,并得到有效的公钥和私钥。

对于非素数p或q,生成密钥对的过程应该失败。

2.加密和解密测试用例:a.输入一个明文数据,用公钥进行加密,然后用私钥进行解密。

b.输入一个密文数据,用私钥进行解密。

测试结果:使用公钥进行加密和私钥进行解密的过程应该成功,并得到相同的明文数据。

用私钥进行解密时,应该得到正确的密文数据。

3.非法密钥测试用例:a.输入一个无效的密钥,测试加密和解密的过程。

b.输入一个被篡改的密钥,测试加密和解密的过程。

测试结果:对于无效的密钥,加密和解密的过程应该失败。

对于被篡改的密钥,加密和解密的过程应该失败或者得到错误的结果。

4.高负载测试用例:a.输入一个大容量的数据,测试加密和解密的性能和效率。

b.输入一个大容量的数据,测试生成密钥对的性能和效率。

测试结果:加密和解密大容量数据的过程应该在合理的时间范围内完成,不应该出现明显的性能瓶颈。

生成密钥对的过程也应该在合理的时间范围内完成。

5.并发性能测试用例:a.同时进行多个加密操作,测试加密的并发性能和效果。

b.同时进行多个解密操作,测试解密的并发性能和效果。

测试结果:当进行多个加密或解密操作时,应该能够正常处理请求,并保持合理的性能水平,不会出现明显的延迟或错误。

通过以上测试用例的设计和执行,可以全面地验证RSA加密算法的正确性、性能以及可靠性。

根据测试结果可以进一步优化算法实现,提高加解密的效率和安全性。

2非对称加密实验

2非对称加密实验
点击“加密”按钮,使用公开密钥Y对明文加密,密文以十六进制形式显示在密文文本框中。
(6)解密
清除明文文本框中的内容,点击“解密”按钮对密文进行解密,明文默认以十六进制形式显示在明文文本框中,如图1.1.720所示;可选择以文本形式查看明文。
(二)扩展实验
(1)在扩展实验中点击“ELGAMAL扩展实验”按钮,进入ELGAMAL扩展实验窗体。
(1)点击“扩展实验”框中的“RSA计算”按钮,进入RSA计算窗体。
输入报文信息,点击“计算MD5值”生成报文信息的信息摘要,选择p、q值,计算n、φ(n)、e和d并输入相应的文本框中,点击“检验”按钮对计算的各个参数值进行检验。
检验无误后,根据上述计算得到的RSA私钥,计算报文MD5值即报文摘要的前8位的签名值,并输入相应的文本框;点击“生成签名并检验”按钮,检验签名输入是否正确并自动生成消息摘要前8位的签名值并显示,
标准方法可选择公钥加密/私钥解密形式和公钥加密/私钥解密形式进行加解密,此处以公钥加密/私钥解密形式进行加解密,公钥加密/私钥解密可参照完成;注意在一次加解密过程中不要重新生成密钥对。
点击“公钥加密”按钮使用生成的公钥对明文进行加密,密文以十六进制显示在密文文本框中;清除明文文本框中的内容,点击“私钥解密”按钮对密文进行解密,明文默认以文本形式显示在明文文本框中,如图1.1.72所示;可选择以16进制查看明文。
具体步骤可参照古典密码实验中实验步骤二。
三、ECC
(一)扩展实验
(1)在扩展实验中点击“ECC扩展实验”按钮,进入ECC扩展实验窗体。
(2)确定合适的椭圆曲线,获取该曲线的基础解点集。首先,在主窗口的椭圆曲线组合框内的相应文本框中,输入素数p,以及椭圆曲线的2个系数a和b;然后,点击“测试曲线”,得到该椭圆曲线的基础解点集。
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

实验二非对称密码算法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落在间隔0*P<n中。

为了做到这一点,只要将明文划分成k 位的块即可,这里k是满足2^k<n的最大整数。

为了加密一个消息P,只要计算C=P^e(mod n) 即可。

为了解密C,只要计算P=C^d(mod n)即可。

可以证明,对于指定范围内的所有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=M^e(mod n)=8M=C^d(mod n)=22、RSA算法与DES算法的比较:运行附件的RSATool,输入一大段文字,记录运行时间。

再使用DES算法加密相同的文字,记录运行时间,对比这两个时间发现,RSA算法比DES算法慢很多,因为RSA算法进行的是大数运算,所以程序运行的速度比DES慢很多。

因此RSA算法只适合于少量数据加密,不适合于大量数据加密。

3、算法设计主要的方法:(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( 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、源程序:(RSA1.java文件)import javax.swing.*;import java.awt.event.*;import java.math.*;import java.util.*;import java.awt.*;import java.io.*;public class RSA1{public static void main(String[] args){MyFrame frame = new MyFrame();MyPanel_fbutton panel_fbutton = new 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);}}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 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;}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 JPanel{public MyPanel_d(JTextField ad){d=ad;add(new JLabel(" 公钥:"));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=JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED;int h=JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED; }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=JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED;int h=JScrollPane.HORIZONTAL_SCROLLBAR_NEVER;}class 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;}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 ;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("解密");}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);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;}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);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;}class 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{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(lerRobin(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;}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(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;}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(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;}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);}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<time; ++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(pareTo(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<inStr.length();i=j){BigInteger t = new BigInteger("0");for(j=i;j<i+nLen&&j<inStr.length();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<m;++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<inStr.length();i+=j){BigInteger t = new BigInteger("0");for(j=0;j<m&&j+i<inStr.length();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(pareTo(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<outStr.length();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(ClassNotFoundException 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);}}}四、实验结果实验结果如下图所示:运行程序,弹出的对话框如上图所示。

相关文档
最新文档