#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
class RSA
{
public:
void getTP();
void displayPUK();
void displayPRK();
unsigned int gcd(unsigned int a, unsigned int b);
long extend( long , long );
unsigned int encrypted(unsigned int m,unsigned int e);
unsigned int decrypted(unsigned int c,unsigned int d);
unsigned int p,q,e,d;
};
void RSA::displayPUK()
{
cout<<"Public Key "<<endl;
cout<<"the value of e and n are : "<<endl;
while(1)
{
e=rand(); /* 确保e 和 (p-1)*(q-1)互素 */
if( e<(p-1)*(q-1) && 1==gcd(e,(p-1)*(q-1)) ) break;
}
cout<<e<<" "<<p*q<<endl;
}
void RSA::displayPRK()
{
cout<<"Private Key "<<endl;
cout<<"the value of d and n are : "<<endl;
d=extend( (p-1)*(q-1),e);
cout<<d<<" "<<p*q<<endl;
}
void RSA::getTP()
{
cout<<"Input tow Primes"<<endl;
cin>>p>>q;
}
unsigned int RSA::gcd(unsigned int a,unsigned int b)
{ unsigned int temp;
if(a<b)
{
temp=a;
a=b;
b=temp;
}
if(b==0)
return a;
else return gcd(b,a%b);
}
long RSA::extend(long a,long e)
{
long x[4],y[4],t[4],q,i;
x[1]=1;
x[2]=0;
x[3]=a;
y[1]=0;
y[2]=1;
y[3]=e;
while(y[3]!=1){
if(y[3]==0){
cout<<"密钥不存在!"<<endl;
break;
}
q=x[3]/y[3];
for(i=1;i<4;i++){
t[i]=x[i]-q*y[i];
x[i]=y[i];
y[i]=t[i];
}
}
if(y[3]==1){
return y[2];
}
}
unsigned int RSA::encrypted(unsigned int m,unsigned int e)
{
unsigned int t=1,temp=m;
while(e!=0)
{
if(e%2!=0)
{
t*=temp;
t%=p*q;
}
e=e>>1;
temp=(temp%(p*q))*(temp%(p*q))%(p*q);
}
return t%(p*q);
}
unsigned int RSA::decrypted(unsigned int c,unsigned int d)
{
unsigned int t=1,temp=c;
while(d!=0)
{
if(d%2!=0)
{
t*=temp;
t%=p*q;
}
d=d>>1;
temp=(temp%(p*q))*(temp%(p*q))%(p*q);
}
return t%(p*q);
}
/*一定要注意明文的值要比n小 */
int main()
{
unsigned int now,rnow;
//char cc[100]="plain.txt",ch;
char cc[100],ch;
RSA rsa;
cout<<"**********************************"<<endl;
rsa.getTP();
cout<<"**********************************"<<endl;
rsa.displayPUK();
cout<<"**********************************"<<endl;
rsa.displayPRK();
cout<<"**********************************"<<endl;
cout<<"Input PlainTextFile "<<endl;
getchar();
cin.getline(cc,100,'\n');
cout<<"***********************************"<<endl;
cout<<"encrypted ........."<<endl;
ifstream infile1(cc);
ofstream outfile1("encrypted.txt");
ofstream outfile2("decrypted.txt");
while(infile1.get(ch))
{
cout<<(unsigned int)ch<<" ";
now=rsa.encrypted((unsigned int )ch,rsa.e); cout<<" now "<<now<<" ";
rnow=rsa.decrypted(now,rsa.d); cout<<" rnow "<<rnow<<endl;
outfile1<<(char)now; //可是这样话c的值有可能超过了256
outfile2<<(char)rnow;
}
infile1.close();
outfile1.close();
outfile2.close();
cout<<"***********************************"<<endl;
cout<<"decrypted..........";
return 0;
}
rsa.rar_扩展欧几里得 算法
版权申诉
103 浏览量
2022-09-24
14:29:08
上传
评论
收藏 1KB RAR 举报
寒泊
- 粉丝: 75
- 资源: 1万+
最新资源
- MyBatis动态SQL是一种强大的特性,它允许我们在SQL语句中根据条件动态地添加或删除某些部分,从而实现更加灵活和高效的数据
- MyBatis动态SQL是一种强大的特性,它允许我们在SQL语句中根据条件动态地添加或删除某些部分,从而实现更加灵活和高效的数据
- 【ERP标准流程-标准流程-进货管理】(DOC 17页).doc
- MyBatis动态SQL是一种强大的特性,它允许我们在SQL语句中根据条件动态地添加或删除某些部分,从而实现更加灵活和高效的数据
- MyBatis动态SQL是一种强大的特性,它允许我们在SQL语句中根据条件动态地添加或删除某些部分,从而实现更加灵活和高效的数据
- Java爬虫项目【项目开发计划】(共12页).docx
- 11111111111
- MyBatis动态SQL是一种强大的特性,它允许我们在SQL语句中根据条件动态地添加或删除某些部分,从而实现更加灵活和高效的数据
- MyBatis动态SQL是一种强大的特性,它允许我们在SQL语句中根据条件动态地添加或删除某些部分,从而实现更加灵活和高效的数据
- MyBatis动态SQL是一种强大的特性,它允许我们在SQL语句中根据条件动态地添加或删除某些部分,从而实现更加灵活和高效的数据
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈