RSA is one of the most powerful methods to encrypt data. The RSA algorithm is described as follow:
> choose two large prime integer p, q
> calculate n = p × q, calculate F(n) = (p - 1) × (q - 1)
> choose an integer e(1 < e < F(n)), making gcd(e, F(n)) = 1, e will be the public key
> calculate d, making d × e mod F(n) = 1 mod F(n), and d will be the private key
M = D(c) = cd mod n
RSA 加密
#include <stdio.h>
int main()
{
int i,j,p,q,e,l,c,d,m;
__int64 fn,n;
while(scanf("%d%d%d%d",&p,&q,&e,&l)!=EOF)
{
n=p*q;
fn=(p-1)*(q-1);
for(i=0;;i++)
if((i*fn+1)%e==0)
{
d=(i*fn+1)/e;
break;
}
for(i=0; i<l; i++)
{
scanf("%d",&c);
m=1;
for(j=1; j<=d; j++)
{
m=(m*c)%n;
}
printf("%c",m);
}
printf("\n");
}
return 0;
}