我自己编写的rsa算法
源代码在线查看: rsa.cpp
#include
#include
#include "isprime.cpp"
#include "gcd.cpp"
void main()
{
//---------------输入两个大素数p,q----------------------
unsigned long p,q; //p,q分别为两个大素数,密钥
unsigned long r; //密钥
unsigned long m,n; //公钥
unsigned long a; //明文
unsigned long b;//密文
unsigned long c;//解密后的内容
unsigned long i;//循环变量
cout cin>>p>>q;
while((!isPrime(p))||(!isPrime(q)))
{
cout cout cout cin>>p>>q;
}
cout //-----------------输入素数r-----------------
cout cin>>r;
cout while(r(p-1)*(q-1)||gcd(r,(p-1)*(q-1))!=1)
{
cout cout cin>>r;
}
//------------求解密钥过程----------
m=1;
while((r*m-1)%((p-1)*(q-1))!=0)
m++;
n=p*q;
cout //------------加密过程------------
cout cin>>a;
cout b=1;
for(i=0;i b=(unsigned long)(a*b);
b=b%n;
cout //--------------解密过程----------
c=1;
for(i=0;i c=(unsigned long)(c*b);
c=c%(p*q);
cout
}