📄 rsa.cpp
字号:
#include "stdafx.h"
#include "RSA.h"
#include<stdlib.h>
#include<time.h>
unsigned long gcd(unsigned long a,unsigned long b);
CRSA::CRSA(void)
{
}
CRSA::~CRSA(void)
{
}
/*
CBigInt CRSA::Gen_Prime(int len)
{
srand((unsigned) time(NULL));
unsigned long tmp,i;
string temp;
i=len/8;
this->number.Clear0();
this->number.m_nLength=i+1;
for(;i>0;i--)
{
tmp=rand()%9973;
tmp*=9973;
tmp+=rand()%9973;
this->number.m_ulvalue[i]=tmp;
}
while((tmp=rand()%9973)%2==0)
{
}
tmp+=(rand()%9973)*9973;
this->number.m_ulvalue[0]=tmp;
while(1)
{
this->number.OutPutToStr(temp,10);
if(this->prime_check(0))
{
cout<<temp<<"是素数"<<endl;
return this->number;
}
cout<<temp<<"是合数"<<endl;
this->number.Add(2);
}
}
*/
CBigInt CRSA::Gen_Prime(int len)
{
unsigned long temp,bigint_len=len/8+1,chengshu,isprime;
string result,tmp_str1,tmp_str2;
CBigInt b,gcdreturn,randtmp;
int gen_prime=0,count;
this->number.Rand(bigint_len);
b.Seed(this->number);
do
{
//this->number.OutPutToStr(result,10);
do
{
b.Mov(b.Rand64());
do
{
randtmp.Mov(b.Rand64());
b.Mov(b.Mul(randtmp));
}while(b.m_nLength<bigint_len);
b.Mov(b.Mod(this->number));
//b.OutPutToStr(tmp_str1,10);
//cout<<result<<" "<<tmp_str1<<" ";
}while(b.Cmp(0)==0);
gcdreturn.Mov(b.Euc(this->number));
//gcdreturn.OutPutToStr(tmp_str2,10);
//cout<<tmp_str2<<endl;
this->number.Mov(this->number.Div(gcdreturn));
b.Mov(b.Div(gcdreturn));
this->number.Mov(this->number.Add(b));
count=0;
gen_prime=0;
while((isprime=this->prime_check(0))==0)
{
this->number.OutPutToStr(result,10);
if(isprime)
{
//cout<<result<<"素数"<<endl;
break;
}
else
{
//cout<<result<<"不是一个素数"<<endl;
count++;
if(count==20)
{
gen_prime=1;
break;
}
}
this->number.Mov(this->number.Add(b));
//cout<<"+1"<<endl;
}
}
while(gen_prime);
//this->number.OutPutToStr(result,10);
//cout<<result<<"是一个素数"<<endl;
return this->number;
}
void CRSA::Gen_Public_Pravite(int len,CBigInt& public_key,CBigInt& private_key,CBigInt& n)
{
CBigInt p,q,by_n,test,tmp;
p.Mov(Gen_Prime(len/2));
q.Mov(Gen_Prime(len/2));
n.Mov(p.Mul(q));
p.Mov(p.Sub(1));
q.Mov(q.Sub(1));
by_n.Mov(p.Mul(q));//by_n=(p-1)*(q-1)
p.~CBigInt();q.~CBigInt();
do
{
test.Rand(len);
test.Mov(test.Mod(by_n));
}
while(test.Cmp(0)==0);
tmp.Mov(test.Euc(by_n));
public_key.Mov(test.Div(tmp));
private_key.Euc(by_n,private_key);
}
void CRSA::Gen_Public_Pravite(int len,string& public_key,string& private_key,string& result_n)
{
CBigInt p,q,n,by_n,public_k,private_k,test,tmp;
p.Mov(Gen_Prime(len/2));
q.Mov(Gen_Prime(len/2));
n.Mov(p.Mul(q));
p.Mov(p.Sub(1));
q.Mov(q.Sub(1));
by_n.Mov(p.Mul(q));//by_n=(p-1)*(q-1)
p.~CBigInt();q.~CBigInt();
do
{
test.Rand(len);
test.Mov(test.Mod(by_n));
}
while(test.Cmp(0)==0);
tmp.Mov(test.Euc(by_n));
public_k.Mov(test.Div(tmp));
public_k.Euc(by_n,private_k);
public_k.OutPutToStr(public_key,10);
private_k.OutPutToStr(private_key,10);
n.OutPutToStr(result_n,10);
}
unsigned long gcd(unsigned long a,unsigned long b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
string CRSA::Encryp(std::string mingwen, std::string public_key, std::string n)
{
CBigInt m,e,mod,result;
string temp;
m.InPutFromStr(mingwen,10);
e.InPutFromStr(public_key,10);
mod.InPutFromStr(n,10);
result.Mov(m.Mon(e,mod));
result.OutPutToStr(temp,10);
return temp;
}
CBigInt CRSA::Encryp(CBigInt mingwen, CBigInt public_key, CBigInt n)
{
CBigInt result;
result.Mov(mingwen.Mon(public_key,n));
return result;
}
string CRSA::Decrypt(std::string miwen, std::string private_key, std::string n)
{
CBigInt c,d,mod,result;
string temp;
c.InPutFromStr(miwen,10);
d.InPutFromStr(private_key,10);
mod.InPutFromStr(n,10);
result.Mov(c.Mon(d,mod));
result.OutPutToStr(temp,10);
return temp;
}
CBigInt CRSA::Decrypt(CBigInt miwen, CBigInt private_key, CBigInt n)
{
CBigInt result;
result.Mov(miwen.Mon(private_key,n));
return result;
}
string CRSA::Sign(std::string text, std::string private_key, std::string n)
{
CBigInt c,d,mod,result;
string temp;
c.InPutFromStr(text,10);
d.InPutFromStr(private_key,10);
mod.InPutFromStr(n,10);
result.Mov(c.Mon(d,mod));
result.OutPutToStr(temp,10);
return temp;
}
CBigInt CRSA::Sign(CBigInt text, CBigInt private_key, CBigInt n)
{
CBigInt result;
result.Mov(text.Mon(private_key,n));
return result;
}
string CRSA::Sign_Check(std::string text, std::string public_key, std::string n)
{
CBigInt m,e,mod,result;
string temp;
m.InPutFromStr(text,10);
e.InPutFromStr(public_key,10);
mod.InPutFromStr(n,10);
result.Mov(m.Mon(e,mod));
result.OutPutToStr(temp,10);
return temp;
}
CBigInt CRSA::Sign_Check(CBigInt text, CBigInt public_key, CBigInt n)
{
CBigInt result;
result.Mov(text.Mon(public_key,n));
return result;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -