#include<"> #include<" />
欢迎来到天天文库
浏览记录
ID:41244886
大小:23.00 KB
页数:5页
时间:2019-08-20
《RSA加密源代码,平方乘算法,素数检测等算法》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、RSA加密源代码,平方乘算法,素数检测等算法.txt和英俊的男人握握手,和深刻的男人谈谈心,和成功的男人多交流,和普通的男人过日子。//RSA.cpp函数功能实现#include"RSA.h"#include#include#include//usingnamespacestd;DataProcess::DataProcess(){n=0;p=q=0;a=b=0;}DataProcess::~DataProcess(){}boolDataProcess::Miller_Rabin(longMn){//prime回答为tru
2、e,compose回答为false,Mn>1longMm=Mn-1;longMa=0;longMb=0;intk=0;//计数器while(!Mm&1)//m&1,偶数为0,奇数为1{//得到奇数m,和2的次幂kMm/=2;++k;}for(intj=0;j<7;++j){//循环7次,正确概率大于99.999%srand((unsigned)time(NULL));//选取时间种子Ma=rand()%(Mn-1)+1;//任选整数数a,13、ue;for(inti=0;i&Bb,intpos,intk)//bitset从pos开始的k位进行伪随机数运算{longBp=0;longBq=0;longBn=0;longs0=0,s=0;longt=0;//do//{//srand((unsigned)time(NULL));//t=rand()%100;/4、/Bp=4*t+3;//}while(!Miller_Rabin(Bp));//p,q均为素数,并且模4余3////do//{//srand((unsigned)time(NULL));//t=rand()%100;//产生两个小随机数//Bq=4*t+3;//保证其模4余3//}while(!Miller_Rabin(Bq));Bp=383;Bq=503;Bn=Bp*Bq;srand((unsigned)time(NULL));s=rand()%(Bn-1)+1;//s取随机数,作为s-1s0=(s*s)%Bn;for(inti=0;i5、)%Bn;s0=s;Bb[pos-i]=s&1;}return;}longDataProcess::Square_Multiply(longbase,longpower,unsignedlongmod){bitset<32>c(power);//将幂次给转化为二进制unsigned__int64z=1;//z初始化为1,定义为unsignedint64防止在乘法时溢出for(inti=31;i>=0;--i){z=(z*z)%mod;if(c[i]==1)z=(z*base)%mod;}return(long)z;}longDataProcess::Euclidean_6、Extend(longEa,longEb)//b在a群中的逆元{//返回它的逆元longa0=Ea;longb0=Eb;longt0=0;longt=1;longEq=(long)(a0/b0);longr=a0-Eq*b0;longtemp=0;while(r>0){temp=(t0-Eq*t)%Ea;t0=t;t=temp;a0=b0;b0=r;Eq=(long)(a0/b0);r=a0-Eq*b0;}if(b0!=1)return-1;//没有逆元else{if(t<0)returnt+Ea;elsereturnt;}}voidDataProcess::RSA_P7、arameter(){unsignedlongFN=0;bitset<32>B_b;bitset<32>B_p,B_q;B_p[7]=1;B_q[6]=1;//p,q分别为8位,7位,并且最高位置1do{Blum_Blum_Shub(B_p,6,7);}while(!Miller_Rabin((long)B_p.to_ulong()));do{Blum_Blum_Shub(B_q,5,6);}while(!Miller_Rabin((long)B_q.to_ulong()));p=(long)B_p.to_ulong();q=(long)
3、ue;for(inti=0;i&Bb,intpos,intk)//bitset从pos开始的k位进行伪随机数运算{longBp=0;longBq=0;longBn=0;longs0=0,s=0;longt=0;//do//{//srand((unsigned)time(NULL));//t=rand()%100;/
4、/Bp=4*t+3;//}while(!Miller_Rabin(Bp));//p,q均为素数,并且模4余3////do//{//srand((unsigned)time(NULL));//t=rand()%100;//产生两个小随机数//Bq=4*t+3;//保证其模4余3//}while(!Miller_Rabin(Bq));Bp=383;Bq=503;Bn=Bp*Bq;srand((unsigned)time(NULL));s=rand()%(Bn-1)+1;//s取随机数,作为s-1s0=(s*s)%Bn;for(inti=0;i5、)%Bn;s0=s;Bb[pos-i]=s&1;}return;}longDataProcess::Square_Multiply(longbase,longpower,unsignedlongmod){bitset<32>c(power);//将幂次给转化为二进制unsigned__int64z=1;//z初始化为1,定义为unsignedint64防止在乘法时溢出for(inti=31;i>=0;--i){z=(z*z)%mod;if(c[i]==1)z=(z*base)%mod;}return(long)z;}longDataProcess::Euclidean_6、Extend(longEa,longEb)//b在a群中的逆元{//返回它的逆元longa0=Ea;longb0=Eb;longt0=0;longt=1;longEq=(long)(a0/b0);longr=a0-Eq*b0;longtemp=0;while(r>0){temp=(t0-Eq*t)%Ea;t0=t;t=temp;a0=b0;b0=r;Eq=(long)(a0/b0);r=a0-Eq*b0;}if(b0!=1)return-1;//没有逆元else{if(t<0)returnt+Ea;elsereturnt;}}voidDataProcess::RSA_P7、arameter(){unsignedlongFN=0;bitset<32>B_b;bitset<32>B_p,B_q;B_p[7]=1;B_q[6]=1;//p,q分别为8位,7位,并且最高位置1do{Blum_Blum_Shub(B_p,6,7);}while(!Miller_Rabin((long)B_p.to_ulong()));do{Blum_Blum_Shub(B_q,5,6);}while(!Miller_Rabin((long)B_q.to_ulong()));p=(long)B_p.to_ulong();q=(long)
5、)%Bn;s0=s;Bb[pos-i]=s&1;}return;}longDataProcess::Square_Multiply(longbase,longpower,unsignedlongmod){bitset<32>c(power);//将幂次给转化为二进制unsigned__int64z=1;//z初始化为1,定义为unsignedint64防止在乘法时溢出for(inti=31;i>=0;--i){z=(z*z)%mod;if(c[i]==1)z=(z*base)%mod;}return(long)z;}longDataProcess::Euclidean_
6、Extend(longEa,longEb)//b在a群中的逆元{//返回它的逆元longa0=Ea;longb0=Eb;longt0=0;longt=1;longEq=(long)(a0/b0);longr=a0-Eq*b0;longtemp=0;while(r>0){temp=(t0-Eq*t)%Ea;t0=t;t=temp;a0=b0;b0=r;Eq=(long)(a0/b0);r=a0-Eq*b0;}if(b0!=1)return-1;//没有逆元else{if(t<0)returnt+Ea;elsereturnt;}}voidDataProcess::RSA_P
7、arameter(){unsignedlongFN=0;bitset<32>B_b;bitset<32>B_p,B_q;B_p[7]=1;B_q[6]=1;//p,q分别为8位,7位,并且最高位置1do{Blum_Blum_Shub(B_p,6,7);}while(!Miller_Rabin((long)B_p.to_ulong()));do{Blum_Blum_Shub(B_q,5,6);}while(!Miller_Rabin((long)B_q.to_ulong()));p=(long)B_p.to_ulong();q=(long)
此文档下载收益归作者所有