📄 rsa.cpp
字号:
// RSA.cpp: implementation of the CRSA class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Cryptology.h"
#include "RSA.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Extended Euclid
//////////////////////////////////////////////////////////////////////
char RSA_HexMap[] = "0123456789abcdef";
typedef struct tagTRIPLE
{
CHuge gcd;
CHuge x;
CHuge y;
}Triple;
Triple Extended_Euclid( CHuge a, CHuge b)
{
Triple r;
if(b==0)
{
r.gcd = a;
r.x = 1;
r.y = 0;
}
else
{
Triple e = Extended_Euclid(b, a%b);
r.gcd = e.gcd;
r.x = e.y;
r.y = e.x-a/b*e.y;
}
return r;
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CRSA::CRSA()
{
m_bInitial = false;
}
CRSA::~CRSA()
{
}
void CRSA::Initial( )
{
srand((unsigned)time(NULL));
CreateRandomHugePrime( m_cP, 1+(RSA_BLOCK_BIT_SIZE>>1) );
CreateRandomHugePrime( m_cQ, RSA_BLOCK_BIT_SIZE>>1 );
m_cN = m_cP*m_cQ;
m_cFN = (m_cP-1)*(m_cQ-1);
m_cE = 0x10001;
Triple tri;
do
{
tri = Extended_Euclid( m_cE, m_cFN );
}
while ( tri.gcd != 1 );
m_cD = tri.x;
if ( m_cD.IsMinus() ) m_cD = m_cD + m_cFN;
m_bInitial = true;
}
bool CRSA::GetInitial( )
{
return m_bInitial;
}
void CRSA::GetKeyN( CString& strKey )
{
RSA_BLOCK b;
m_cN.TransToBytes( (BYTE*)b, RSA_BLOCK_BYTE_SIZE+1 );
strKey.Empty( );
for ( int i = 0; i <= RSA_BLOCK_BYTE_SIZE; i++ )
{
strKey += RSA_HexMap[b[i]>>4];
strKey += RSA_HexMap[b[i]&0xf];
strKey += ' ';
}
strKey += '\0';
}
//////////////////////////////////////////////////////////////////////
// encrypt
//////////////////////////////////////////////////////////////////////
void CRSA::TransMsgToBlocks( CString strMsg )
{
int i, j, l, t;
l = strMsg.GetLength( );
m_iBlocks = l/RSA_BLOCK_BYTE_SIZE;
for( i=0; i<m_iBlocks; i++ )
{
t = i*RSA_BLOCK_BYTE_SIZE;
for( j=0; j<RSA_BLOCK_BYTE_SIZE; j++ )
m_pBlocks[i][j] = strMsg[t+j];
m_pBlocks[i][RSA_BLOCK_BYTE_SIZE] = 0;
}
if( m_iBlocks*RSA_BLOCK_BYTE_SIZE < l )
{
t = (m_iBlocks)*RSA_BLOCK_BYTE_SIZE;
for( j=0; j<RSA_BLOCK_BYTE_SIZE; j++ )
{
if( t+j >= l )
m_pBlocks[m_iBlocks][j] = 0;
else
m_pBlocks[m_iBlocks][j] = strMsg[t+j];
}
m_pBlocks[m_iBlocks][RSA_BLOCK_BYTE_SIZE] = 0;
m_iBlocks++;
}
}
void CRSA::TransBlocksToCryptInHex( CString& strCrypt )
{
int i, t;
strCrypt.Empty( );
t = m_iBlocks*RSA_BLOCK_BYTE_SIZE;
for( i=0; i<t; i++ )
{
strCrypt += RSA_HexMap[(*((BYTE*)m_pBlocks+i))>>4];
strCrypt += RSA_HexMap[(*((BYTE*)m_pBlocks+i))&0xf];
strCrypt += ' ';
}
}
void CRSA::EncryptBlock( RSA_BLOCK b )
{
CHuge crypt;
crypt.CreateFromBytes( (BYTE*)b, RSA_BLOCK_BYTE_SIZE+1 );
crypt = PowerMod( crypt, m_cE, m_cN );
crypt.TransToBytes( (BYTE*)b, RSA_BLOCK_BYTE_SIZE+1 );
}
void CRSA::Encrypt( CString strMsg, CString& strCrypt )
{
TransMsgToBlocks( strMsg );
for( int i=0; i<m_iBlocks; i++ )
EncryptBlock( m_pBlocks[i] );
TransBlocksToCryptInHex( strCrypt );
}
//////////////////////////////////////////////////////////////////////
// decrypt
//////////////////////////////////////////////////////////////////////
void CRSA::DecryptBlock( RSA_BLOCK b )
{
CHuge decry;
decry.CreateFromBytes( (BYTE*)b, RSA_BLOCK_BYTE_SIZE+1 );
decry = PowerMod( decry, m_cD, m_cN );
decry.TransToBytes( (BYTE*)b, RSA_BLOCK_BYTE_SIZE+1 );
}
void CRSA::TransBlocksToDecrypt( CString& strDecrypt)
{
int i, j;
strDecrypt.Empty( );
for( i=0; i<m_iBlocks; i++ )
{
for( j=0; j<RSA_BLOCK_BYTE_SIZE; j++ )
strDecrypt += m_pBlocks[i][j];
}
}
void CRSA::Decrypt( CString strCrypt, CString& strDecrypt )
{
for( int i=0; i<m_iBlocks; i++ )
DecryptBlock( m_pBlocks[i] );
TransBlocksToDecrypt( strDecrypt );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -