还剩11页未读,继续阅读
本资源只提供10页预览,全部文档请下载后查看!喜欢就下载吧,查找使用更方便
文本内容:
云南大学数学与统计学实验教学中心实验报告课程名称近代密码学年级级2010上机实践成绩指导教师陆正福姓名段飞龙上机实践名称实现学号RSA20101910050上机实践日期上机实践编号组号上机实践时间No.1
一、实验目的配置环境,并对其进行测试;通过实现加密、解密gmp gmpRSA
二、实验内容、配置环境,并对其进行测试;1gmp、编程实现非对称密码算法2RSA
三、实验环境UbuntuVC
四、实验结果对的测试结果如下
1.gmp)对加法运算进行测试的结果1the nis1the sumis0the nis101the sumis338350the nis1001the sumis333833500the nis10001the sumis333383335000the nis100001the sumis333338333350000the nis1000001the sumis333333833333500000the nis10000001the sumis333333383333335000000the nis100000001the sumis333333338333333350000000the nis1000000001the sumis333333333833333333500000000)对乘法运算进行测试的结果:21234567823456789the resultis
289589963907942.测试输入的字符串为最大长度(在此例中取最大长度为)的字符串时的结果:
2.10()Input maxlengthof message:IMain menu:int flag,flagl;char ch,flagchar;unsigned intL=OJ=O;mpz_t d,e,n,c,cin;mpz_initd;mpz_inite;mpz_initii;mpz_initc;mpz_initcin;printfHInput maxlengthof message:H;scanfn%uH,L;getchar;//l=100;RSA_gmpd,e,n,L;while1flagl=l;printfHMain menu:\nH;printfn\t[l]Encryption!\nH;printfH\t
[2]Deciphering!\nH;printfn\t
[3]Out putthe secretkey ofencryption!\nH;printfn\t
[4]Out putthe secretkey ofdeciphering!\nH;printfH\t
[0]Exit!\nn;printfHInput youroption:;scanfH%d,\flag;getchar;m=char*mallocsizeofchar*L+l;switch flagcase1:printfHInput amessage:\nM;i=l;whilel{scanfn%cn,ch;ifch!=An!{m[O]=ch;break;}else printfnThemessage cannot empty!\nlnput amessage:\nn;}scanfH%cH,ch;whilech!=\n{m[i++]=ch;scanfn%cM,ch;m[i]=,\0,;l=strlenm;encryptionm dn cj;999gmp_printf*1The ciphertextis c:\n%Zd\nH,c;break;case2:printfnInput theciphertext八n;gmp_scanfn%Zdn,cin;getchar;decipheringm,e,n,c,8^1;//Need exchange1break;case3:printfHThe secretkey ofencryption isd,n:\nH;gmp_printfH%Zd,\n%Zd\n”,d,n;break;case4:printfnThe secretkey ofdecipering ise,n:\nH;gmp_printfH%Zd,\n%Zd\n*\e,n;break;case0:flagl=0;break;default:printfnError data!\nn;break;}if flagl==0break;}}
六、教师评语
[1]Encryption!
[2]Deciphering!
[3]Out putthe secretkey ofencryption!
[4]Out putthe secretkey ofdeciphering!
[0]Exit!Input youroption:3The secretkey ofencryption isd,n:Main menu:
[1]Encryption!
[2]Deciphering!
[3]Out putthe secretkey ofencryption!
[4]Out putthe secretkey ofdeciphering!
[0]Exit!Input youroption:4The secretkey ofdecipering ise,n:Main menu:
[1]Encryption!
[2]Deciphering!
[3]Out putthe secretkey ofencryption!
[4]Out putthe secretkey ofdeciphering!
[0]Exit!Input youroption:1Input amessage:1234567890The ciphertextis c:Main menu:
[1]Encryption!
[2]Deciphering!
[3]Out putthe secretkey ofencryption!
[4]Out putthe secretkey ofdeciphering!
[0]Exit!Input youroption:Input theciphertext:message is1234567890Main menu:
[1]Encryption![2|Deciphering!
[3]Out putthe secretkey ofencryption!
[4]Out putthe secretkey ofdeciphering!
[0]Exit!Input youroption测试输入的字符串小于最大长度在此例中取最大长度为的字符串时的结果
3.50Input maxlengthof message:5Main menu:
[1]Encryption![2|Deciphering!
[3]Out putthe secretkey ofencryption!
[4]Out putthe secretkey ofdeciphering!
[0]Exit!Input youroption:3The secretkey ofencryption isd,n:Main menu:
[1]Encryption!
[2]Deciphering!
[3]Out putthe secretkey ofencryption!
[4]Out putthe secretkey ofdeciphering!
[0]Exit!Input youroption:4The secretkey ofdecipering ise,n:Main menu:
[1]Encryption!
[2]Deciphering!
[3]Out putthe secretkey ofencryption!
[4]Out putthe secretkey ofdeciphering!
[0]Exit!Input youroption:!Input amessage:I amin Yunnanuniversity!The ciphertextis c:Main menu:
[1]Encryption![2|Deciphering!
[3]Out putthe secretkey ofencryption!
[4]Out putthe secretkey ofdeciphering!
[0]Exit!Input youroption:Input theciphertext:message isI amin Yunnanuniversity!Main menu:
[1]Encryption!
[2]Deciphering!
[3]Out putthe secretkey ofencryption!
[4]Out putthe secretkey ofdeciphering!
[0]Exit!Input youroption:注绿色字体为输入内容拷贝到里后,经手动修改word
五、程序源代码.对测试的源代码1gmp加法1#includegmp.h#includestdio.h voidmain mpz_t s,n;int i,j=0,m=10;mpz_inits;//init s,the valueis0mpz_initn;fori=l;illllllllll;i++mpz_add_uin,n,1;//set n to n+1ifj%m==0{gmp_printfnthe nis%Zd\n\n;gmp_printfuthe sumis%Zd\n*,s;m=m*10;mpz_addmu1s,n,n;//add n*ntos j++;gmp_printfthe sumis%Zd\nu,s;mpz_clears;乘法2#includegmp.h#includestdio.hchar*BigMulchar*m,char*n;int mainchar*p=NULL;;char*a=12345678”;char*b=23456789”p=BigMula,b;printfnthe resultis%s.\n,p;return0;char*BigMulchar*m,char*nint i,j;char*pt=NULL;mpz_t s,p,q;mpz_inits;i=mpz_init_set_strp,m,10;//get numberfrom mj=mpz_init_set_strq,n,10;〃printf”i,j:%d,%d\n\i,j;gmp_printfn%Zd\n%Zd\nH,p,q;mpz_addmuls,p,q;//calculate result//gmp_printfthe resultis%Zd\n\s;pt=mpz_get_strpt,10,s;//get stringfrom s//printfn%s\nn,pt;mpz_clears;return pt;源代码
2.RSA#includegmp.h#includemath.h#includestdio.h#includemalloc.h#includestring.h voidRSA_gmpmpz_t*d,mpz_t*e,mpz_t*n unsigned int1{//generate secretkey d,e,nchar*str;int size;unsigned long10,seed;mpz_t p,q,fii,fn_r;10=4*1+10;mpzjnitp;mpz_initq;mpz_initfn;mpz_initfn_r;str=char Hcmallocsizeofchar*2*10+3;在定义时,生成随机数用后是定值//seed=rand%l«31+100;//seed randseedgmp_randstate_t state;gmp_randinit_defaultstate;gmp_randseed_uistate,seed++;mpz_urandombp,stateJO;mpz_nextprimep,p;gmp_randseed_uistate,seed++;mpz_urandombq,state,10;mpz_nextprimeq,q;mpz_mul*n,p,q;//Calculate forn=p*qmpz_sub_uip,p,l;mpz_sub_uiq,q,l;mpz_mulfn,p,q;//Calculate forfn=p-l*q-lmpz_sqrtfn_r,fn;mpz_get_strstr,2,fn;10=strlenstr;whilel{//Geberate esqrtfnefn gmp_randseed_uistate++seed;9mpz_urandomb*e,state J0・2;mpz_nextprime*e,*e;if*efn_r break;}』mpz_invert*d,*e n;void encryptionchar.message,mpz_t d,mpz_t n,mpz_t unsignedint1{//str[N];//N=8000000has testedint i=0J,temp;char*str,*p;mpz_t m;p=message;str=char*mallocsizeofchar*8*l+l;while*p{//Turn astring tobinarytemp=*p;forj=7;j=0;j»str[i+j]=0,+temp%2;temp/=2;;P++i+=8;}mpz_init_set_strm,str,2;mpz_powm*c,m,d,n;void decipheringchar^message,mpz_t e,mpz_t n,mpz_t c,unsignedint1intitemp num;99char str[l],*p;mpz_t m;mpz_initm;mpz_powmm,c,e,n;mpz_get_strstr,2,m;l=strlenstr;temp=l%8-l;p=message;*p=0;fori=0;iI;i++=*p*2+str[i]・O;ifi-temp%8==0{*++p=0;}*p=\0;printfHmessage is%s\nn,message;void maininti;。
个人认证
优秀文档
获得点赞 0