📄 rsa.cpp
字号:
#include "stdafx.h"
#include "Rsa.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
vlong public_key::encrypt( const vlong& text )
{
vlong out;
return ::modexp(text,e,n,out);
}
//-----------------------------------------------------------------------------
private_key::private_key()
{
}
private_key::~private_key()
{
}
vlong private_key::decrypt( const vlong& cipher )
{
vlong out;
return ::modexp(cipher,d,n,out);
}
bool private_key::create( )//vlong& p, vlong& q, vlong& d, vlong& n, vlong& e)
{
int p_len=16;
int q_len=16;
int e_len=16;
vlong m;
if(!::findprime(p,p_len))
return false;
if(!::findprime(q,q_len))
return false;
unsigned a1 = p;
unsigned a2 = q;
n = p*q;
m = (p - vlong(1))*(q - vlong(1));
vlong gcd;
while(1)
{
if(!::randval(e,e_len))
return false;
if(e >= m)
continue;
gcd_cl(e.m_clint,m.m_clint,gcd.m_clint);
if(gcd != vlong(1))
continue;
if(!::modinv2(e,m,d))
continue;
if( ((e*d)%m) != vlong(1) )
continue; //MessageBox(NULL,"求逆出错","警告",0);
if( e < n && d < n && d > vlong(0) )
{
break;
}
}
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -