?? rsasimple.txt
字號:
#include iostream.h
#include conio.h
#include math.h
#include stdlib.h
#include string.h
int Transform(int m,int k,int n);
int IsRelativelyPrime(int a,int b);
int IsPrime(int a);
void main()
{
int X,Y,e,d,n,En,p,q;
couttSimple Demo of RSA Algorithmnn;
coutPlease enter the value of p ;
cinp;
while(!IsPrime(p))
{
coutError! p should be prime ;
cinp;
}
coutPlease enter the value of q ;
cinq;
while(!IsPrime(q))
{
coutError! q should be prime ;
cinq;
}
coutnCalculating n and ф(n)...endl;
n=pq;
En=(p-1)(q-1);
coutn = n and ф(n) = Enendl;
coutendlPlease enter the value of e ;
cine;
while (!IsRelativelyPrime(e,En))
{
coute should be relatively prime to n ;
cine;
}
coutCalculating d...endl;
d=1;
while (((ed)%En)!=1) d++;
cout d = d endl;
coutendlPlease enter the value of Xendl;
cinX;
while (X=nX=0)
{
cout aX should be within 0 and n ;
cinX;
}
coutEncrypting message...endl;
Y=Transform(X,e,n);
coutCipher Y = Yendl;
coutDecrypting cipher...endl;
X=Transform(Y,d,n);
coutX = Xendl;
coutnPress any key to quit.endl;
getch();
}
int Transform(int m,int k,int n)
{
long int r=1;
char bk[16];
itoa(k,bk,2);
for (unsigned int i=0;istrlen(bk);i++)
{
r=(rr)%n;
if (bk[i] == '1')
{
r=(rm)%n;
}
}
return r;
}
int IsRelativelyPrime(int a,int b)
{
for(int i=2;i=sqrt(abab);i++)
if ((a%i == 0)&&(b%i==0))
return 0;
return 1;
}
int IsPrime(int a)
{
for(int i=2;i=sqrt(a);i++)
if(a%i==0) return 0;
return 1;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -