Abstract Algebra chapter 7

时间:2021-11-11 07:20:52

7.7:Encrypt each of the following RSA messages x so that x is divided into blocks of integers of length 2; that is, if x = 142528,encode 14,25,and 28 separately.

RSA加密方法:y=x^E mod n

计算时可采用重复乘方法(repeated squares)

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
using namespace std; int binary[];
double mod[]; int main()
{
int n,E,x;
while ((cin>>n>>E>>x)&&n&&E&&x)
{
memset(binary,,sizeof(binary));
memset(mod,,sizeof(mod));
for (int i=;i>=;i--)
{
if (<<i <= E)
{
binary[i]=;
E-= (<<i);
}
if (E<=0.001) break;
}
mod[]=x%n;
for (int i=;i<sizeof(binary);i++)
{
mod[i]=fmod(pow(mod[i-],),n);
}
double temp = ;
for (int i=;i<sizeof(binary);i++)
{
if (binary[i]==)
{
temp = fmod(fmod(temp,n)*fmod(mod[i],n),n);
}
}
cout<<temp<<endl;
}
return ;
}