欢迎来到天天文库
浏览记录
ID:38168582
大小:26.49 KB
页数:7页
时间:2019-06-06
《RSA算法实验代码》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、RSA算法实验代码#include#defineMAX_NUM63001#defineMAX_PRIME251//!返回代码#defineOK100#defineERROR_NOEACHPRIME101#defineERROR_NOPUBLICKEY102#defineERROR_GENERROR103unsignedintMakePrivatedKeye(unsignedintuiP,unsignedintuiQ);unsignedintGetPrivateKeye(unsignedinti
2、Which);unsignedintMakePairkey(unsignedintuiP,unsignedintuiQ,unsignedintuiD);unsignedintGetPairKey(unsignedint&d,unsignedint&e);intrsa_encrypt(intn,inte,intmw,intp,intq);intrsa_decrypt(intn,intd,intcw,intp,intq);voidoutputkey();//! 保存私钥d集合structpKeyset{unsign
3、edintset[MAX_NUM];unsignedintsize;}pset;//!保存公、私钥对structpPairkey{unsignedintd;unsignedinte;unsignedintn;}pairkey;//名称:isPrime//功能:判断两个数是否互质//参数:m:数a;n:数b//返回:m、n互质返回true;否则返回falseboolisPrime(unsignedintm,unsignedintn){unsignedinti=0;boolFlag=true;if(m<2
4、
5、n<2
6、)returnfalse;unsignedinttem=(m>n)?n:m;for(i=2;i<=tem&&Flag;i++){boolmFlag=true;boolnFlag=true;if(m%i==0)mFlag=false;if(n%i==0)nFlag=false;if(!mFlag&&!nFlag)Flag=false;}if(Flag)returntrue;elsereturnfalse;}//名称:MakePrivatedKeye//功能:由素数P、Q生成公钥e//参数:uiP:素数P;uiQ:
7、素数Q//返回:私钥dunsignedintMakePrivatedKeye(unsignedintuiP,unsignedintuiQ){unsignedinti=0;//!得到所有与z互质的数(私钥d的集合)unsignedintz=(uiP-1)*(uiQ-1);pset.size=0;for(i=0;i8、数:uiP:素数P;uiQ:素数Q;uiD:私钥d//返回:错误代码unsignedintMakePairkey(unsignedintuiP,unsignedintuiQ,unsignedintuiD){boolbFlag=true;unsignedinti=0,d;unsignedintz=(uiP-1)*(uiQ-1);unsignedinte=pset.set[uiD];if(!isPrime(z,e))returnERROR_NOEACHPRIME;for(i=2;i9、z==1){d=i;bFlag=false;}}if(bFlag)returnERROR_NOPUBLICKEY;if((d*e)%z!=1)ERROR_GENERROR;pairkey.d=d;pairkey.e=e;pairkey.n=uiP*uiQ;returnOK;}//名称:GetPairKey//功能:对外提供接口,获得公、私钥对//参数:uiP:素数P;uiQ:素数Q;uiD:私钥d//返回:unsignedintGetPairKey(unsignedint&d,unsignedint&e){d=10、pairkey.d;e=pairkey.e;returnpairkey.n;}//名称:GetPrivateKeye//功能:对外提供接口,由用户选择ID得以私钥d//参数:iWhich:用户选择公钥e的ID//返回:私钥d值unsignedintGetPrivateKeye(unsignedintiWhich){if(pset.size>=iWhich)returnpset.set[
8、数:uiP:素数P;uiQ:素数Q;uiD:私钥d//返回:错误代码unsignedintMakePairkey(unsignedintuiP,unsignedintuiQ,unsignedintuiD){boolbFlag=true;unsignedinti=0,d;unsignedintz=(uiP-1)*(uiQ-1);unsignedinte=pset.set[uiD];if(!isPrime(z,e))returnERROR_NOEACHPRIME;for(i=2;i9、z==1){d=i;bFlag=false;}}if(bFlag)returnERROR_NOPUBLICKEY;if((d*e)%z!=1)ERROR_GENERROR;pairkey.d=d;pairkey.e=e;pairkey.n=uiP*uiQ;returnOK;}//名称:GetPairKey//功能:对外提供接口,获得公、私钥对//参数:uiP:素数P;uiQ:素数Q;uiD:私钥d//返回:unsignedintGetPairKey(unsignedint&d,unsignedint&e){d=10、pairkey.d;e=pairkey.e;returnpairkey.n;}//名称:GetPrivateKeye//功能:对外提供接口,由用户选择ID得以私钥d//参数:iWhich:用户选择公钥e的ID//返回:私钥d值unsignedintGetPrivateKeye(unsignedintiWhich){if(pset.size>=iWhich)returnpset.set[
9、z==1){d=i;bFlag=false;}}if(bFlag)returnERROR_NOPUBLICKEY;if((d*e)%z!=1)ERROR_GENERROR;pairkey.d=d;pairkey.e=e;pairkey.n=uiP*uiQ;returnOK;}//名称:GetPairKey//功能:对外提供接口,获得公、私钥对//参数:uiP:素数P;uiQ:素数Q;uiD:私钥d//返回:unsignedintGetPairKey(unsignedint&d,unsignedint&e){d=
10、pairkey.d;e=pairkey.e;returnpairkey.n;}//名称:GetPrivateKeye//功能:对外提供接口,由用户选择ID得以私钥d//参数:iWhich:用户选择公钥e的ID//返回:私钥d值unsignedintGetPrivateKeye(unsignedintiWhich){if(pset.size>=iWhich)returnpset.set[
此文档下载收益归作者所有