RSA算法基础--实践
|
来源:xfocus.org 作者:watercloud 发布时间:2005-03-09
|
|
RSA算法基础--实践讲讲自己学习RSA中的实践过程,已经对RSA熟悉的看家就不用在此浪费时间了。一基础RSA算法非常简单,概述如下:找两素数p和q取n=p*q取t=(p-1)*(q-1)取任何一个数e,要求满足e实践接下来我们来一个实践,看看实际的操作:找两个素数:p=47q=59这样n=p*q=2773t=(p-1)*(q-1)=2668取e=63,满足eperl -e "foreach $i (1..9999){ print($i),last if $i*63%2668==1 }"847即d=847最终我们获得关键的n=2773d=847e=63取消息M=244我们看看加密:c=M**d%n = 244**847%2773用perl的大数计算来算一下:C:\Temp>perl -Mbigint -e "print 244**847%2773"465即用d对M加密后获得加密信息c=465解密:我们可以用e来对加密后的c进行解密,还原M:m=c**e%n=465**63%2773 :C:\Temp>perl -Mbigint -e "print 465**63%2773"244即用e对c解密后获得m=244 , 该值和原始信息M相等。<三>字符串加密把上面的过程集成一下我们就能实现一个对字符串加密解密的示例了。每次取字符串中的一个字符的ascii值作为M进行计算,其输出为加密后16进制的数的字符串形式,按3字节表示,如01F代码如下:#!/usr/bin/perl -w#RSA 计算过程学习程序编写的测试程序#watercloud 2003-8-12#use strict;use Math::BigInt;my %RSA_CORE = (n=>2773,e=>63,d=>847); #p=47,q=59my $N=new Math::BigInt($RSA_CORE{n});my $E=new Math::BigInt($RSA_CORE{e});my $D=new Math::BigInt($RSA_CORE{d});print "N=$N D=$D E=$E\n";sub RSA_ENCRYPT {my $r_mess = shift @_;my ($c,$i,$M,$C,$cmess);for($i=0;$i < length($$r_mess);$i++){$c=ord(substr($$r_mess,$i,1));$M=Math::BigInt->new($c);$C=$M->copy(); $C->bmodpow($D,$N);$c=sprintf "%03X",$C;$cmess.=$c;}return \$cmess;}sub RSA_DECRYPT {my $r_mess = shift @_;my ($c,$i,$M,$C,$dmess);for($i=0;$i < length($$r_mess);$i+=3){$c=substr($$r_mess,$i,3);$c=hex($c);$M=Math::BigInt->new($c);$C=$M->copy(); $C->bmodpow($E,$N);$c=chr($C);$dmess.=$c;}return \$dmess;}my $mess="RSA 娃哈哈哈~~~";$mess=$ARGV[0] if @ARGV >= 1;print "原始串:",$mess,"\n";my $r_cmess = RSA_ENCRYPT(\$mess);print "加密串:",$$r_cmess,"\n";my $r_dmess = RSA_DECRYPT($r_cmess);print "解密串:",$$r_dmess,"\n";#EOF测试一下:C:\Temp>perl rsa-test.plN=2773 D=847 E=63原始串:RSA 娃哈哈哈~~~加密串:5CB6CD6BC58A7709470AA74A0AA74A0AA74A6C70A46C70A46C70A4解密串:RSA 娃哈哈哈~~~C:\Temp>perl rsa-test.pl 安全焦点(xfocus)N=2773 D=847 E=63原始串:安全焦点(xfocus)加密串:3393EC12F0A466E0AA9510D025D7BA0712DC3379F47D51C325D67B解密串:安全焦点(xfocus)<四>提高前面已经提到,rsa的安全来源于n足够大,我们测试中使用的n是非常小的,根本不能保障安全性,我们可以通过RSAKit、RSATool之类的工具获得足够大的N 及D E。通过工具,我们获得1024位的N及D E来测试一下:n=0x328C74784DF31119C526D18098EBEBB943B0032B599CEE13CC2BCE7B5FCD15F90B66EC3A85F5005DBDCDED9BDFCB3C4C265AF164AD55884D8278F791C7A6BFDAD55EDBC4F017F9CCF1538D4C2013433B383B47D80EC74B51276CA05B5D6346B9EE5AD2D7BE7ABFB36E37108DD60438941D2ED173CCA50E114705D7E2BC511951d=0x10001e=0xE760A3804ACDE1E8E3D7DC0197F9CEF6282EF552E8CEBBB7434B01CB19A9D87A3106DD28C523C29954C5D86B36E943080E4919CA8CE08718C3B0930867A98F635EB9EA9200B25906D91B80A47B77324E66AFF2C4D70D8B1C69C50A9D8B4B7A3C9EE05FFF3A16AFC023731D80634763DA1DCABE9861A4789BD782A592D2B1965设原始信息M=0x11111111111122222222222233333333333完成这么大数字的计算依赖于大数运算库,用perl来运算非常简单:A) 用d对M进行加密如下:c=M**d%n :C:\Temp>perl -Mbigint -e " $x=Math::BigInt->bmodpow(0x11111111111122222222222233333333333, 0x10001, 0x328C74784DF31119C526D18098EBEBB943B0032B599CEE13CC2BCE7B5FCD15F90B66EC3A85F5005DBDCDED9BDFCB3C4C265AF164AD55884D8278F791C7A6BFDAD55EDBC4F017F9CCF1538D4C2013433B383B47D80EC74B51276CA05B5D6346B9EE5AD2D7BE7ABFB36E37108DD60438941D2ED173CCA50E114705D7E2BC511951);print $x->as_hex"0x17b287be418c69ecd7c39227ab681ac422fcc84bb35d8a632543b304de288a8d4434b73d2576bd45692b007f3a2f7c5f5aa1d99ef3866af26a8e876712ed1d4cc4b293e26bc0a1dc67e247715caa6b3028f9461a3b1533ec0cb476441465f10d8ad47452a12db0601c5e8beda686dd96d2acd59ea89b91f1834580c3f6d90898即用d对M加密后信息为:c=0x17b287be418c69ecd7c39227ab681ac422fcc84bb35d8a632543b304de288a8d4434b73d2576bd45692b007f3a2f7c5f5aa1d99ef3866af26a8e876712ed1d4cc4b293e26bc0a1dc67e247715caa6b3028f9461a3b1533ec0cb476441465f10d8ad47452a12db0601c5e8beda686dd96d2acd59ea89b91f1834580c3f6d90898B) 用e对c进行解密如下:m=c**e%n :C:\Temp>perl -Mbigint -e " $x=Math::BigInt->bmodpow(0x17b287be418c69ecd7c39227ab681ac422fcc84bb35d8a632543b304de288a8d4434b73d2576bd45692b007f3a2f7c5f5aa1d99ef3866af26a8e876712ed1d4cc4b293e26bc0a1dc67e247715caa6b3028f9461a3b1533ec0cb476441465f10d8ad47452a12db0601c5e8beda686dd96d2acd59ea89b91f1834580c3f6d90898, 0xE760A3804ACDE1E8E3D7DC0197F9CEF6282EF552E8CEBBB7434B01CB19A9D87A3106DD28C523C29954C5D86B36E943080E4919CA8CE08718C3B0930867A98F635EB9EA9200B25906D91B80A47B77324E66AFF2C4D70D8B1C69C50A9D8B4B7A3C9EE05FFF3A16AFC023731D80634763DA1DCABE9861A4789BD782A592D2B1965, 0x328C74784DF31119C526D18098EBEBB943B0032B599CEE13CC2BCE7B5FCD15F90B66EC3A85F5005DBDCDED9BDFCB3C4C265AF164AD55884D8278F791C7A6BFDAD55EDBC4F017F9CCF1538D4C2013433B383B47D80EC74B51276CA05B5D6346B9EE5AD2D7BE7ABFB36E37108DD60438941D2ED173CCA50E114705D7E2BC511951);print $x->as_hex"0x11111111111122222222222233333333333(我的P4 1.6G的机器上计算了约5秒钟)得到用e解密后的m=0x11111111111122222222222233333333333 == MC) RSA通常的实现RSA简洁幽雅,但计算速度比较慢,通常加密中并不是直接使用RSA 来对所有的信息进行加密,最常见的情况是随机产生一个对称加密的密钥,然后使用对称加密算法对信息加密,之后用RSA对刚才的加密密钥进行加密。最后需要说明的是,当前小于1024位的N已经被证明是不安全的自己使用中不要使用小于1024位的RSA,最好使用2048位的。watercloud [at] xfocus.org2005-2-21
|
|
|
[推荐]
[评论(0条)]
[返回顶部] [打印本页]
[关闭窗口] |
|
|
|
|
|
|
推荐广告 |
|
|
|
|