</pre>AesEncrypt.h<pre name="code" class="cpp">//enum KeySize { Bits128, Bits192, Bits256 }; // key size, in bits, for construtor
#define Bits128 16
#define Bits192 24
#define Bits256 32
#define BASE_LEN_128 16 // 128位下每次加密数据最大字节为16
static unsigned char AesSbox[16*16] =
{// populate the Sbox matrix
/* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
/*0*/ 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
/*1*/ 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
/*2*/ 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
/*3*/ 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
/*4*/ 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
/*5*/ 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
/*6*/ 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
/*7*/ 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
/*8*/ 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
/*9*/ 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
/*a*/ 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
/*b*/ 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
/*c*/ 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
/*d*/ 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
/*e*/ 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
/*f*/ 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16
};
static unsigned char AesiSbox[16*16] =
{
// populate the iSbox matrix
/* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
/*0*/ 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
/*1*/ 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
/*2*/ 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
/*3*/ 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25,
/*4*/ 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92,
/*5*/ 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
/*6*/ 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06,
/*7*/ 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b,
/*8*/ 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
/*9*/ 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e,
/*a*/ 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b,
/*b*/ 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
/*c*/ 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f,
/*d*/ 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef,
/*e*/ 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
/*f*/ 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d
};
static unsigned char AesRcon[11*4] =
{
0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00,
0x02, 0x00, 0x00, 0x00,
0x04, 0x00, 0x00, 0x00,
0x08, 0x00, 0x00, 0x00,
0x10, 0x00, 0x00, 0x00,
0x20, 0x00, 0x00, 0x00,
0x40, 0x00, 0x00, 0x00,
0x80, 0x00, 0x00, 0x00,
0x1b, 0x00, 0x00, 0x00,
0x36, 0x00, 0x00, 0x00
};
class CAesEncrypt
{
public:
CAesEncrypt();
~CAesEncrypt();
CAesEncrypt(int keySize, unsigned char* keyBytes);
public:
void Cipher(unsigned char* input, unsigned char* output); // encipher 16-bit input
void InvCipher(unsigned char* input, unsigned char* output); // decipher 16-bit input
private:
void SetNbNkNr(int keySize);
void AddRoundKey(int round); // 轮密钥加
void SubBytes(); // S盒字节代换
void InvSubBytes(); // 逆S盒字节代换
void ShiftRows(); // 行移位
void InvShiftRows();
void MixColumns(); // 列混淆
void InvMixColumns();
unsigned char gfmultby01(unsigned char b);
unsigned char gfmultby02(unsigned char b);
unsigned char gfmultby03(unsigned char b);
unsigned char gfmultby09(unsigned char b);
unsigned char gfmultby0b(unsigned char b);
unsigned char gfmultby0d(unsigned char b);
unsigned char gfmultby0e(unsigned char b);
void KeyExpansion(); // 密钥扩展
unsigned char* SubWord(unsigned char* word); // 密钥S盒字代换
unsigned char* RotWord(unsigned char* word); // 密钥移位
public:
unsigned char State[4][4];
private:
int Nb; // block size in 32-bit words. Always 4 for AES. (128 bits).
int Nk; // key size in 32-bit words. 4, 6, 8. (128, 192, 256 bits).
int Nr; // number of rounds. 10, 12, 14.
unsigned char key[32];
unsigned char w[16*15];
};
int char2num(char ch);
AesEncrypt.cpp
#include "StdAfx.h" //注意在此 #include "Aes.h"不可以放在前面,否则出错,
#include "AesEncrypt.h"
CAesEncrypt::CAesEncrypt()
{
}
CAesEncrypt::~CAesEncrypt()
{
}
CAesEncrypt::CAesEncrypt(int keysize, unsigned char* keyBytes)
{
SetNbNkNr(keysize); //设置密钥块数,轮数
memcpy(key,keyBytes,keysize); //字符串拷贝函数,把keyBytes的keysize个字符复制到key中
KeyExpansion(); //密钥扩展,必须提前做的初始化
}
void CAesEncrypt::SetNbNkNr(int keySize)
{
Nb = 4;
if(keySize=Bits128)
{
Nk=4; //4*4字节,128位密钥,10轮加密
Nr=10;
}
else if(keySize=Bits192)
{
Nk=6; //6*4字节,192位密钥,12轮加密
Nr=12;
}
else if(keySize=Bits256)
{
Nk=8; //8*4字节,256位密钥,14轮加密
Nr=14;
}
}
void CAesEncrypt::KeyExpansion()
{
memset(w,0,16*15);
for(int row=0;row<Nk;row++) //拷贝seed 密钥
{
w[4*row+0] = key[4*row];
w[4*row+1] = key[4*row+1];
w[4*row+2] = key[4*row+2];
w[4*row+3] = key[4*row+3];
}
byte* temp = new byte[4];
for(int row=Nk;row<4*(Nr+1);row++)
{
temp[0]=w[4*row-4]; //当前列的前一列
temp[1]=w[4*row-3];
temp[2]=w[4*row-2];
temp[3]=w[4*row-1];
if(row%Nk==0) //逢nk时,对当前列的前一列作特殊处理
{
temp=SubWord(RotWord(temp)); //先移位,再代换,最后和轮常量异或
temp[0] = (byte)( (int)temp[0] ^ (int) AesRcon[4*(row/Nk)+0] );
temp[1] = (byte)( (int)temp[1] ^ (int) AesRcon[4*(row/Nk)+1] );
temp[2] = (byte)( (int)temp[2] ^ (int) AesRcon[4*(row/Nk)+2] );
temp[3] = (byte)( (int)temp[3] ^ (int) AesRcon[4*(row/Nk)+3] );
}
else if ( Nk > 6 && (row % Nk == 4) ) //这个还没有搞清楚
{
temp = SubWord(temp);
}
// w[row] = w[row-Nk] xor temp
w[4*row+0] = (byte) ( (int) w[4*(row-Nk)+0] ^ (int)temp[0] );
w[4*row+1] = (byte) ( (int) w[4*(row-Nk)+1] ^ (int)temp[1] );
w[4*row+2] = (byte) ( (int) w[4*(row-Nk)+2] ^ (int)temp[2] );
w[4*row+3] = (byte) ( (int) w[4*(row-Nk)+3] ^ (int)temp[3] );
} // for loop
}
//密钥移位函数
unsigned char* CAesEncrypt::RotWord(unsigned char* word)
{
byte* temp = new byte[4];
temp[0] = word[1];
temp[1] = word[2];
temp[2] = word[3];
temp[3] = word[0];
return temp;
}
//密钥字代换函数
unsigned char* CAesEncrypt::SubWord(unsigned char* word)
{
byte* temp = new byte[4];
for(int j=0;j<4;j++)
{
temp[j] = AesSbox[16*(word[j] >> 4)+(word[j] & 0x0f)]; //实际上也可以写成AesSbox[[j]];因为两者相等
}
return temp;
}
//Aes加密函数
void CAesEncrypt::Cipher(unsigned char* input, unsigned char* output)
{
memset(&State[0][0],0,16);
for(int i=0;i<4*Nb;i++) //这里是先写列后写行的,即输入是一列一列的进来的
{
State[i%4][i/4]=input[i]; //换成先写行后写列也是可以的,只要在输出时也是这样就可以了
}
AddRoundKey(0); //轮密钥加
for (int round = 1; round <= (Nr - 1); round++) // main round loop
{
SubBytes(); //字节代换
ShiftRows(); //行移位
MixColumns(); //列混淆
AddRoundKey(round); //轮密钥加
} // main round loop
SubBytes(); //字节代换
ShiftRows(); //行移位
AddRoundKey(Nr); //轮密钥加
// output = state
for (int i = 0; i < (4 * Nb); i++)
{
output[i] = State[i % 4][ i / 4];
}
}
//Aes解密函数
void CAesEncrypt::InvCipher(unsigned char* input,unsigned char* output)
{
memset(&State[0][0],0,16);
for (int i = 0; i < (4 * Nb); i++)
{
State[i % 4][ i / 4] = input[i];
}
AddRoundKey(Nr);
for (int round = Nr-1; round >= 1; round--) // main round loop
{
InvShiftRows();
InvSubBytes();
AddRoundKey(round);
InvMixColumns();
} // end main round loop for InvCipher
InvShiftRows();
InvSubBytes();
AddRoundKey(0);
// output = state
for (int i = 0; i < (4 * Nb); i++)
{
output[i] = State[i % 4][ i / 4];
}
}
//轮密钥加
void CAesEncrypt::AddRoundKey(int round)
{
int i,j; //i行 j列 //因为密钥w是一列一列排列的,即 k0 k4 k8 k12
for(j=0;j<4;j++) // k1 k5 k9 k13
{ // k2 k6 k10k14
for(i=0;i<4;i++) // k3 k7 k11k15
{ // 所以i行j列的下标是4*((round*4)+j)+i即16*round+4*j+i
State[i][j]=(unsigned char)((int)State[i][j]^(int)w[4*((round*4)+j)+i]);
}
}
}
//字节代换函数
void CAesEncrypt::SubBytes() //Page 103
{
int i,j;
for(j=0;j<4;j++)
{
for(i=0;i<4;i++)
{
State[i][j]=AesSbox[State[i][j]];
//因为 16*(State[i][j]>>4)+State[i][j]&0x0f=State[i][j]
}
}
}
void CAesEncrypt::InvSubBytes()
{
int i,j;
for(j=0;j<4;j++)
{
for(i=0;i<4;i++)
{
State[i][j]=AesiSbox[State[i][j]]; //因为 16*(State[i][j]>>4)+State[i][j]&0x0f=State[i][j]
}
}
}
void CAesEncrypt::ShiftRows()
{
unsigned char temp[4*4]; //Page105
int i,j;
for(j=0;j<4;j++)
{
for(i=0;i<4;i++)
{
temp[4*i+j]=State[i][j];
}
}
for(i=1;i<4;i++)
{
for(j=0;j<4;j++)
{
if(i==1)State[i][j]=temp[4*i+(j+1)%4]; //第一行左移1位
else if(i==2)State[i][j]=temp[4*i+(j+2)%4]; //第二行左移2位
else if(i==3)State[i][j]=temp[4*i+(j+3)%4]; //第三行左移3位
}
}
}
void CAesEncrypt::InvShiftRows()
{
unsigned char temp[4*4];
int i,j;
for(j=0;j<4;j++)
{
for(i=0;i<4;i++)
{
temp[4*i+j]=State[i][j];
}
}
for(i=1;i<4;i++)
{
for(j=0;j<4;j++)
{
//if(i==1)State[i][j]=temp[4*i+(j-1)%4]; 在此犯了一个错误 -1%4=-1 而不是3,所以采用了下面再加一个4的做法
if(i==1)State[i][j]=temp[4*i+(j+3)%4]; //第一行右移1位 j-1+4=j+3
else if(i==2)State[i][j]=temp[4*i+(j+2)%4]; //第二行右移2位 j-2+4=j+2
else if(i==3)State[i][j]=temp[4*i+(j+1)%4]; //第三行右移3位 j-3+4=j+2
}
}
}
void CAesEncrypt::MixColumns()
{
unsigned char temp[4*4];
int i,j;
for(j=0;j<4;j++) //2 3 1 1 列混淆矩阵 Page107
{ //1 2 3 1
for(i=0;i<4;i++) //1 1 2 3
{ //3 1 1 2
temp[4*i+j]=State[i][j];
}
}
for(j=0;j<4;j++)
{
State[0][j] = (unsigned char) ( (int)gfmultby02(temp[0+j]) ^ (int)gfmultby03(temp[4*1+j]) ^
(int)gfmultby01(temp[4*2+j]) ^ (int)gfmultby01(temp[4*3+j]) );
State[1][j] = (unsigned char) ( (int)gfmultby01(temp[0+j]) ^ (int)gfmultby02(temp[4*1+j]) ^
(int)gfmultby03(temp[4*2+j]) ^ (int)gfmultby01(temp[4*3+j]) );
State[2][j] = (unsigned char) ( (int)gfmultby01(temp[0+j]) ^ (int)gfmultby01(temp[4*1+j]) ^
(int)gfmultby02(temp[4*2+j]) ^ (int)gfmultby03(temp[4*3+j]) );
State[3][j] = (unsigned char) ( (int)gfmultby03(temp[0+j]) ^ (int)gfmultby01(temp[4*1+j]) ^
(int)gfmultby01(temp[4*2+j]) ^ (int)gfmultby02(temp[4*3+j]) );
}
}
void CAesEncrypt::InvMixColumns()
{
unsigned char temp[4*4];
int i,j;
for (i = 0; i < 4; i++) // copy State into temp[]
{
for (j = 0; j < 4; j++) //0e 0b 0d 09 逆变换矩阵 Page108
{ //09 0e 0b 0d
temp[4*i+j] = State[i][j]; //0d 09 0e 0b
} //0b 0d 09 0e
}
for (j = 0; j < 4; j++)
{
State[0][j] = (unsigned char) ( (int)gfmultby0e(temp[j]) ^ (int)gfmultby0b(temp[4+j]) ^
(int)gfmultby0d(temp[4*2+j]) ^ (int)gfmultby09(temp[4*3+j]) );
State[1][j] = (unsigned char) ( (int)gfmultby09(temp[j]) ^ (int)gfmultby0e(temp[4+j]) ^
(int)gfmultby0b(temp[4*2+j]) ^ (int)gfmultby0d(temp[4*3+j]) );
State[2][j] = (unsigned char) ( (int)gfmultby0d(temp[j]) ^ (int)gfmultby09(temp[4+j]) ^
(int)gfmultby0e(temp[4*2+j]) ^ (int)gfmultby0b(temp[4*3+j]) );
State[3][j] = (unsigned char) ( (int)gfmultby0b(temp[j]) ^ (int)gfmultby0d(temp[4+j]) ^
(int)gfmultby09(temp[4*2+j]) ^ (int)gfmultby0e(temp[4*3+j]) );
}
}
unsigned char CAesEncrypt::gfmultby01(unsigned char b)
{
return b;
}
unsigned char CAesEncrypt::gfmultby02(unsigned char b)
{
if (b < 0x80)
return (unsigned char)(int)(b <<1);
else
return (unsigned char)( (int)(b << 1) ^ (int)(0x1b) );
}
unsigned char CAesEncrypt::gfmultby03(unsigned char b)
{
return (unsigned char) ( (int)gfmultby02(b) ^ (int)b );
}
unsigned char CAesEncrypt::gfmultby09(unsigned char b)
{
return (unsigned char)( (int)gfmultby02(gfmultby02(gfmultby02(b))) ^ (int)b );
}
unsigned char CAesEncrypt::gfmultby0b(unsigned char b)
{
return (unsigned char)( (int)gfmultby02(gfmultby02(gfmultby02(b))) ^
(int)gfmultby02(b) ^ (int)b );
}
unsigned char CAesEncrypt::gfmultby0d(unsigned char b)
{
return (unsigned char)( (int)gfmultby02(gfmultby02(gfmultby02(b))) ^
(int)gfmultby02(gfmultby02(b)) ^ (int)(b) );
}
unsigned char CAesEncrypt::gfmultby0e(unsigned char b)
{
return (unsigned char)( (int)gfmultby02(gfmultby02(gfmultby02(b))) ^
(int)gfmultby02(gfmultby02(b)) ^(int)gfmultby02(b) );
}
//字符ASCII码值到字符字面值的转换 如 '0'转换成0, 'a'转换成10
int char2num(char ch)
{
if(ch>='0'&&ch<='9')return ch-'0';
else if(ch>='a'&&ch<='f')return ch-'a'+10;
return -1;
}
BigInt.h
</pre><pre name="code" class="cpp">// ------------------------------------------------------------------------
//§ File: BigInt.h
//§
//§ Desc: CBigInt类 大数运算类
//§ 适用于1024位RSA运算
//§
//§ Date: 2011/3/9
//§ Copyright(c) xychzh
// ------------------------------------------------------------------------
#pragma once
#define BI_MAXLEN 35 // 最高只支持1120位的大数(35*4*8=1120)
#define DEC 10
#define HEX 16
// 素数的位数
#define PRIME_BITS_128 128/2
#define PRIME_BITS_256 256/2
#define PRIME_BITS_512 512/2
#define PRIME_BITS_1024 1024/2
//-----------------------------------------------------------------------------
// CBigInt类
//-----------------------------------------------------------------------------
class CBigInt
{
public:
CBigInt();
~CBigInt();
public:
// 数值运算
void Mov(unsigned __int64 A); // 赋值运算,可赋值为大数或普通整数,可重载为运算符“=”
void Mov(CBigInt& A);
CBigInt Add(CBigInt& A); // 加,求大数与大数或大数与普通整数的和,可重载为运算符“+”
CBigInt Add(unsigned long A);
CBigInt Sub(CBigInt& A); // 减,求大数与大数或大数与普通整数的差,可重载为运算符“-”
CBigInt Sub(unsigned long A);
CBigInt Mul(CBigInt& A); // 乘,求大数与大数或大数与普通整数的积,可重载为运算符“*”
CBigInt Mul(unsigned long A);
CBigInt Div(CBigInt& A); // 除,求大数与大数或大数与普通整数的商,可重载为运算符“/”
CBigInt Div(unsigned long A);
CBigInt Mod(CBigInt& A); // 模,求大数与大数或大数与普通整数的模,可重载为运算符“%”
unsigned long Mod(unsigned long A);
int Cmp(CBigInt& A); // 比较运算,可重载为运算符“==”、“!=”、“>=”、“<=”等
public:
void SetValueFromNumber(unsigned __int64 srcData);
void SetValueFromHexStr(char* pSrcData, int len); // 将字符串按10进制或16进制格式输入到大数
void GetValueToStr(char* pDstStr, unsigned int system=HEX); // 将大数按10进制或16进制格式输出到字符串
public:
// RSA相关运算
void CreatePrime(int Bits); // 产生指定长度的随机大素数
int Rab(); // 拉宾米勒算法进行素数测试
CBigInt Euc(CBigInt& A); // 欧几里德算法求解同余方程
CBigInt RsaTrans(CBigInt& A, CBigInt& B); // 反复平方算法进行幂模运算
public:
// 每个元素精度为32位,若使用128位密钥,则素数的位数为64位(两个64位的素数相乘得到一个128位的数),然后两个数组元素便可组成一个素数
unsigned long m_ulValue[BI_MAXLEN]; // 存放大数值的数组
unsigned int m_nLength; // 数值长度
};
// ------------------------------------------------------------------------
//§ File: BigInt.cpp
//§
//§ Desc: CBigInt类 大数运算类
//§ 适用于1024位RSA运算
//§
//§ Date: 2011/3/9
//§ Copyright(c) xychzh
// ------------------------------------------------------------------------
#include "stdafx.h"
#include "BigInt.h"
//小素数表
const static int PrimeTable[550] =
{
3, 5, 7, 11, 13, 17, 19, 23, 29, 31,
37, 41, 43, 47, 53, 59, 61, 67, 71, 73,
79, 83, 89, 97, 101, 103, 107, 109, 113, 127,
131, 137, 139, 149, 151, 157, 163, 167, 173, 179,
181, 191, 193, 197, 199, 211, 223, 227, 229, 233,
239, 241, 251, 257, 263, 269, 271, 277, 281, 283,
293, 307, 311, 313, 317, 331, 337, 347, 349, 353,
359, 367, 373, 379, 383, 389, 397, 401, 409, 419,
421, 431, 433, 439, 443, 449, 457, 461, 463, 467,
479, 487, 491, 499, 503, 509, 521, 523, 541, 547,
557, 563, 569, 571, 577, 587, 593, 599, 601, 607,
613, 617, 619, 631, 641, 643, 647, 653, 659, 661,
673, 677, 683, 691, 701, 709, 719, 727, 733, 739,
743, 751, 757, 761, 769, 773, 787, 797, 809, 811,
821, 823, 827, 829, 839, 853, 857, 859, 863, 877,
881, 883, 887, 907, 911, 919, 929, 937, 941, 947,
953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019,
1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087,
1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153,
1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229,
1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297,
1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381,
1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453,
1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523,
1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597,
1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663,
1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741,
1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823,
1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901,
1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993,
1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063,
2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131,
2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221,
2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293,
2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371,
2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437,
2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539,
2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621,
2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, 2689,
2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749,
2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, 2833,
2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903, 2909,
2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001,
3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, 3083,
3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187,
3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, 3259,
3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343,
3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433,
3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517,
3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581,
3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, 3659,
3671, 3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727, 3733,
3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, 3821, 3823,
3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, 3911,
3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, 4001
};
//-----------------------------------------------------------------------------
// CBigInt类
//-----------------------------------------------------------------------------
CBigInt::CBigInt()
{
m_nLength = 1;
for (int i=0; i<BI_MAXLEN; i++)
{
m_ulValue[i] = 0;
}
}
CBigInt::~CBigInt()
{
}
// 大数比较
// 调用方式:N.Cmp(A)
// 返回值:若N<A返回-1;若N=A返回0;若N>A返回1
int CBigInt::Cmp(CBigInt& A)
{
if (m_nLength > A.m_nLength) return 1;
if (m_nLength < A.m_nLength) return -1;
for (int i=m_nLength-1; i>=0; i--)
{
if (m_ulValue[i] > A.m_ulValue[i]) return 1;
if (m_ulValue[i] < A.m_ulValue[i]) return -1;
}
return 0;
}
// 大数赋值
// 调用方式:N.Mov(A)
// 返回值:无,N被赋值为A
void CBigInt::Mov(CBigInt& A)
{
m_nLength = A.m_nLength;
for (int i=0; i<BI_MAXLEN; i++)
{
m_ulValue[i] = A.m_ulValue[i];
}
}
void CBigInt::Mov(unsigned __int64 A)
{
if (A > 0xffffffff)
{
m_nLength = 2;
m_ulValue[1] = (unsigned long)(A >> 32);
m_ulValue[0] = (unsigned long)A;
}
else
{
m_nLength = 1;
m_ulValue[0] = (unsigned long)A;
}
for(int i=m_nLength; i<BI_MAXLEN; i++)
{
m_ulValue[i] = 0;
}
}
// 大数相加
// 调用形式:N.Add(A)
// 返回值:N+A
CBigInt CBigInt::Add(CBigInt& A)
{
CBigInt X;
X.Mov(*this);
unsigned carry = 0;
unsigned __int64 sum = 0;
if (X.m_nLength < A.m_nLength)
{
X.m_nLength = A.m_nLength;
}
for (unsigned i=0; i<X.m_nLength; i++)
{
sum = A.m_ulValue[i];
sum = sum+X.m_ulValue[i] + carry;
X.m_ulValue[i] = (unsigned long)sum;
carry = (unsigned)(sum >> 32);
}
X.m_ulValue[X.m_nLength] = carry;
X.m_nLength += carry;
return X;
}
CBigInt CBigInt::Add(unsigned long A)
{
CBigInt X;
X.Mov(*this);
unsigned __int64 sum;
sum = X.m_ulValue[0];
sum += A;
X.m_ulValue[0] = (unsigned long)sum;
if (sum > 0xffffffff)
{
unsigned i = 1;
while (X.m_ulValue[i] == 0xffffffff)
{
X.m_ulValue[i] = 0;
i++;
}
X.m_ulValue[i]++;
if (m_nLength == i)
{
m_nLength++;
}
}
return X;
}
// 大数相减
// 调用形式:N.Sub(A)
// 返回值:N-A
CBigInt CBigInt::Sub(CBigInt& A)
{
CBigInt X;
X.Mov(*this);
if (X.Cmp(A) <= 0)
{
X.Mov(0);
return X;
}
unsigned carry = 0;
unsigned __int64 num;
for (unsigned int i=0; i<m_nLength; i++)
{
if ((m_ulValue[i]>A.m_ulValue[i])||((m_ulValue[i]==A.m_ulValue[i])&&(carry==0)))
{
X.m_ulValue[i] = m_ulValue[i]-carry-A.m_ulValue[i];
carry = 0;
}
else
{
num = 0x100000000+m_ulValue[i];
X.m_ulValue[i] = (unsigned long)(num-carry-A.m_ulValue[i]);
carry = 1;
}
}
while (X.m_ulValue[X.m_nLength-1] == 0)
{
X.m_nLength--;
}
return X;
}
CBigInt CBigInt::Sub(unsigned long A)
{
CBigInt X;
X.Mov(*this);
if (X.m_ulValue[0] >= A)
{
X.m_ulValue[0] -= A;
return X;
}
if (X.m_nLength == 1)
{
X.Mov(0);
return X;
}
unsigned __int64 num = 0x100000000 + X.m_ulValue[0];
X.m_ulValue[0] = (unsigned long)(num-A);
int i = 1;
while (X.m_ulValue[i] == 0)
{
X.m_ulValue[i] = 0xffffffff;
i++;
}
X.m_ulValue[i]--;
if (X.m_ulValue[i] == 0)
{
X.m_nLength--;
}
return X;
}
// 大数相乘
// 调用形式:N.Mul(A)
// 返回值:N*A
CBigInt CBigInt::Mul(CBigInt& A)
{
if (A.m_nLength == 1) return Mul(A.m_ulValue[0]);
CBigInt X;
unsigned __int64 sum = 0;
unsigned __int64 mul = 0;
unsigned __int64 carry = 0;
X.m_nLength = m_nLength+A.m_nLength-1;
for (unsigned int i=0; i<X.m_nLength; i++)
{
sum = carry;
carry = 0;
for (unsigned int j=0; j<A.m_nLength; j++)
{
if (i-j>=0 && i-j<m_nLength)
{
mul = m_ulValue[i-j];
mul *= A.m_ulValue[j];
carry += mul >> 32;
mul = mul & 0xffffffff;
sum += mul;
}
}
carry += sum >> 32;
X.m_ulValue[i] = (unsigned long)sum;
}
if (carry != 0)
{
X.m_nLength++;
X.m_ulValue[X.m_nLength-1] = (unsigned long)carry;
}
return X;
}
CBigInt CBigInt::Mul(unsigned long A)
{
CBigInt X;
unsigned __int64 mul;
unsigned long carry = 0;
X.Mov(*this);
for (unsigned int i=0; i<m_nLength; i++)
{
mul = m_ulValue[i];
mul = mul*A + carry;
X.m_ulValue[i] = (unsigned long)mul;
carry = (unsigned long)(mul >> 32);
}
if (carry != 0)
{
X.m_nLength++;
X.m_ulValue[X.m_nLength-1] = carry;
}
return X;
}
// 大数相除
// 调用形式:N.Div(A)
// 返回值:N/A
CBigInt CBigInt::Div(CBigInt& A)
{
if (A.m_nLength == 1) return Div(A.m_ulValue[0]);
CBigInt X,Y,Z;
unsigned int i, len;
unsigned __int64 num, div;
Y.Mov(*this);
while (Y.Cmp(A) >= 0)
{
div = Y.m_ulValue[Y.m_nLength-1];
num = A.m_ulValue[A.m_nLength-1];
len = Y.m_nLength - A.m_nLength;
if (div==num && len==0)
{
X.Mov(X.Add(1));
break;
}
if (div<=num && len!=0)
{
len--;
div = (div<<32) + Y.m_ulValue[Y.m_nLength-2];
}
div = div / (num+1);
Z.Mov(div);
if (len != 0)
{
Z.m_nLength += len;
for (i=Z.m_nLength-1; i>=len; i--)
{
Z.m_ulValue[i] = Z.m_ulValue[i-len];
}
for (i=0; i<len; i++)
{
Z.m_ulValue[i] = 0;
}
}
X.Mov(X.Add(Z));
Y.Mov(Y.Sub(A.Mul(Z)));
}
return X;
}
CBigInt CBigInt::Div(unsigned long A)
{
CBigInt X;
X.Mov(*this);
if (X.m_nLength == 1)
{
X.m_ulValue[0] = X.m_ulValue[0] / A;
return X;
}
unsigned __int64 div, mul;
unsigned long carry = 0;
for(int i=X.m_nLength-1; i>=0; i--)
{
div = carry;
div = (div<<32) + X.m_ulValue[i];
X.m_ulValue[i] = (unsigned long)(div/A);
mul = (div/A)*A;
carry = (unsigned long)(div-mul);
}
if (X.m_ulValue[X.m_nLength-1] == 0)
{
X.m_nLength--;
}
return X;
}
// 大数求模
// 调用形式:N.Mod(A)
// 返回值:N%A
CBigInt CBigInt::Mod(CBigInt& A)
{
CBigInt X, Y;
unsigned __int64 div, num;
unsigned long carry = 0;
unsigned i, len;
X.Mov(*this);
while (X.Cmp(A) >= 0)
{
div = X.m_ulValue[X.m_nLength-1];
num = A.m_ulValue[A.m_nLength-1];
len = X.m_nLength - A.m_nLength;
if (div==num && len==0)
{
X.Mov(X.Sub(A));
break;
}
if (div<=num && len)
{
len--;
div = (div<<32) + X.m_ulValue[X.m_nLength-2];
}
div = div / (num+1);
Y.Mov(div);
Y.Mov(A.Mul(Y));
if (len != 0)
{
Y.m_nLength += len;
for (i=Y.m_nLength-1; i>=len; i--)
{
Y.m_ulValue[i] = Y.m_ulValue[i-len];
}
for (i=0; i<len; i++)
{
Y.m_ulValue[i] = 0;
}
}
X.Mov(X.Sub(Y));
}
return X;
}
unsigned long CBigInt::Mod(unsigned long A)
{
if (m_nLength == 1) return (m_ulValue[0] % A);
unsigned __int64 div;
unsigned long carry = 0;
for (int i=m_nLength-1; i>=0; i--)
{
div = m_ulValue[i];
div += carry * 0x100000000;
carry = (unsigned long)(div%A);
}
return carry;
}
// 将srcData的每一位数字分解出来,依次存储到m_ulValue中
// srcData为十进制数
void CBigInt::SetValueFromNumber(unsigned __int64 srcData)
{
Mov(srcData);
}
// 将pSrcData的每一位字符转换为十进制数字,依次存储到m_ulValue中
// pSrcData中存储的是十六进制数字,如:"80FF7C"
void CBigInt::SetValueFromHexStr(char* pSrcData, int len)
{
Mov(0);
for (int i=0, n=0; i<len; i++)
{
if (pSrcData[i]>='0'&& pSrcData[i]<='9') n = pSrcData[i] - 48;
else if (pSrcData[i]>='A' && pSrcData[i]<='F') n = pSrcData[i] - 55;
else if (pSrcData[i]>='a' && pSrcData[i]<='f') n = pSrcData[i] - 87;
else break; // 正常情况是这里应该是终止符
Mov(Mul(HEX));
Mov(Add(n));
}
}
// 将大数按10进制或16进制格式输出为字符串
// 调用格式:N.GetValueToStr(str,sys)
// 返回值:无,参数str被赋值为N的sys进制字符串
// sys暂时只能为10或16
void CBigInt::GetValueToStr(char* pDstStr, unsigned int system)
{
if (m_nLength==1 && m_ulValue[0]==0)
{
pDstStr[0] = '\0';
return;
}
CBigInt X;
X.Mov(*this);
char valueMap[] = "0123456789ABCDEF";
char chDst[128+1];
int index = 0;
for (int num; X.m_ulValue[X.m_nLength-1]>0; )
{
num = X.Mod(system);
chDst[index++] = valueMap[num];
X.Mov(X.Div(system));
}
for (int i=0; i<index; i++)
{
pDstStr[i] = chDst[index-1-i];
}
pDstStr[index] = '\0';
}
// 求不定方程ax-by=1的最小整数解
// 调用方式:N.Euc(A)
// 返回值:X,满足:NX mod A=1
CBigInt CBigInt::Euc(CBigInt& A)
{
CBigInt M, E, X, Y, I, J;
int x = 1;
int y = 1;
M.Mov(A);
E.Mov(*this);
X.Mov(0);
Y.Mov(1);
while (E.m_nLength!=1 || E.m_ulValue[0]!=0)
{
I.Mov(M.Div(E));
J.Mov(M.Mod(E));
M.Mov(E);
E.Mov(J);
J.Mov(Y);
Y.Mov(Y.Mul(I));
if (x == y)
{
if (X.Cmp(Y) >= 0)
{
Y.Mov(X.Sub(Y));
}
else
{
Y.Mov(Y.Sub(X));
y=0;
}
}
else
{
Y.Mov(X.Add(Y));
x = 1-x;
y = 1-y;
}
X.Mov(J);
}
if (x==0)
{
X.Mov(A.Sub(X));
}
return X;
}
// 求乘方的模
// 加密公式【 C ≡ (M^e) mod n 】
// 解密公式【 M ≡ (C^d) mod n 】
// 返回C或M,即明文或密文
CBigInt CBigInt::RsaTrans(CBigInt& A, CBigInt& B)
{
CBigInt X, Y;
int i, j, k;
unsigned n;
unsigned long num;
k = A.m_nLength*32 - 32;
num = A.m_ulValue[A.m_nLength-1];
while (num != 0)
{
num = num >> 1;
k++;
}
X.Mov(*this);
for (i=k-2; i>=0; i--)
{
Y.Mov(X.Mul(X.m_ulValue[X.m_nLength-1]));
Y.Mov(Y.Mod(B));
for (n=1; n<X.m_nLength; n++)
{
for (j=Y.m_nLength; j>0; j--)
{
Y.m_ulValue[j] = Y.m_ulValue[j-1];
}
Y.m_ulValue[0] = 0;
Y.m_nLength++;
Y.Mov(Y.Add(X.Mul(X.m_ulValue[X.m_nLength-n-1])));
Y.Mov(Y.Mod(B));
}
X.Mov(Y);
if ((A.m_ulValue[i>>5] >> (i&31)) & 1)
{
Y.Mov(Mul(X.m_ulValue[X.m_nLength-1]));
Y.Mov(Y.Mod(B));
for (n=1; n<X.m_nLength; n++)
{
for (j=Y.m_nLength; j>0; j--)
{
Y.m_ulValue[j] = Y.m_ulValue[j-1];
}
Y.m_ulValue[0] = 0;
Y.m_nLength++;
Y.Mov(Y.Add(Mul(X.m_ulValue[X.m_nLength-n-1])));
Y.Mov(Y.Mod(B));
}
X.Mov(Y);
}
}
return X;
}
// 拉宾米勒算法测试素数
// 调用方式:N.Rab()
// 返回值:若N为素数,返回1,否则返回0
int CBigInt::Rab()
{
unsigned i, j, pass;
for (i=0; i<550; i++)
{
if (Mod(PrimeTable[i]) == 0) return 0;
}
CBigInt S, A, I, K;
K.Mov(*this);
K.m_ulValue[0]--;
for (i=0; i<5; i++)
{
pass = 0;
A.Mov(rand()*rand());
S.Mov(K);
while ((S.m_ulValue[0]&1) == 0)
{
for (j=0; j<S.m_nLength; j++)
{
S.m_ulValue[j] = S.m_ulValue[j] >> 1;
if (S.m_ulValue[j+1] & 1)
{
S.m_ulValue[j] = S.m_ulValue[j] | 0x80000000;
}
}
if (S.m_ulValue[S.m_nLength-1] == 0)
{
S.m_nLength--;
}
I.Mov(A.RsaTrans(S,*this));
if (I.Cmp(K) == 0)
{
pass=1;
break;
}
}
if (I.m_nLength==1 && I.m_ulValue[0]==1)
{
pass = 1;
}
if (pass == 0)
{
return 0;
}
}
return 1;
}
// 产生随机素数
// 调用方法:N.CreatePrime(bits)
// 返回值:N被赋值为一个bits位(0x100000000进制长度)的素数
void CBigInt::CreatePrime(int Bits)
{
m_nLength = Bits/32;
begin:
for (unsigned int i=0; i<m_nLength; i++)
{
m_ulValue[i] = rand()*0x10000 + rand();
}
m_ulValue[0] = m_ulValue[0] | 1;
for (unsigned int i=m_nLength-1; i>0; i--)
{
m_ulValue[i] = m_ulValue[i] << 1;
if (m_ulValue[i-1] & 0x80000000)
{
m_ulValue[i]++;
}
}
m_ulValue[0] = m_ulValue[0] << 1;
m_ulValue[0]++;
for (unsigned int i=0;i<550; i++)
{
if (Mod(PrimeTable[i]) == 0)
{
goto begin;
}
}
CBigInt S, A, I, K;
K.Mov(*this);
K.m_ulValue[0]--;
for (unsigned int i=0; i<5; i++)
{
A.Mov(rand()*rand());
S.Mov(K.Div(2));
I.Mov(A.RsaTrans(S, *this));
if (((I.m_nLength!=1) || (I.m_ulValue[0]!=1)) && (I.Cmp(K)!=0))
{
goto begin;
}
}
}
RsaEncrypt.h
// ------------------------------------------------------------------------
//§ File: RsaEncrypt.h
//§
//§ Desc: CRsaEncrypt类 RSA非对称加密类
//§
//§ Date: 2011/3/8
//§ Copyright(c) xychzh
// ------------------------------------------------------------------------
#pragma once
#include "BigInt.h"
#define KEY_BITS_128 128
#define KEY_BITS_256 256
#define KEY_BITS_512 512
#define KEY_BITS_1024 1024
#define MAX_KEY_LEN KEY_BITS_128/4 // 目前仅使用128位密钥进行加/解密
#define MAX_PACK_SIZE 1024/4 // 最大支持1M大小的包数据
// 密钥(存储的是十六进制大数)
struct SRSAKEY
{
char m_Key_x[MAX_KEY_LEN+1]; // 公钥则为e,私钥则为d
char m_Key_n[MAX_KEY_LEN+1];
};
struct SRSADATA
{
char m_Data[MAX_PACK_SIZE][MAX_KEY_LEN+1]; // 包数据,包含终止符
};
//-----------------------------------------------------------------------------
// 【CRsaEncrypt RSA加密/解密类】
//
// [执行流程]
// 1:解密者调用CreateKey()生成公钥和私钥,然后将公钥发送给加密者
// 2:加密者通过收到的公钥对数据进行加密操作,然后将数据发送给解密者
// 3:解密者使用自己私钥对数据进行解密
//-----------------------------------------------------------------------------
class CRsaEncrypt
{
public:
CRsaEncrypt();
~CRsaEncrypt();
public:
void CreateKey();
bool Encrypt(unsigned __int64 srcData, char* dstData, SRSAKEY publicKey);
bool Decrypt(char* srcData, int len, char* dstData, SRSAKEY key);
SRSAKEY GetPublicKey() { return m_PublicKey; }
SRSAKEY GetPrivateKey() { return m_PrivateKey; }
private:
SRSAKEY m_PublicKey; // 公钥 (e,n)
SRSAKEY m_PrivateKey; // 私钥 (d,n)
CBigInt m_p;
CBigInt m_q;
CBigInt m_e;
CBigInt m_n;
CBigInt m_d;
};
RsaEncrypt.cpp
// ------------------------------------------------------------------------
//§ File: RsaEncrypt.cpp
//§
//§ Desc: CRsaEncrypt类 RSA非对称加密类
//§
//§ Date: 2011/3/8
//§ Copyright(c) xychzh
// ------------------------------------------------------------------------
#include "stdafx.h"
#include "RsaEncrypt.h"
CRsaEncrypt::CRsaEncrypt()
{
}
CRsaEncrypt::~CRsaEncrypt()
{
}
// 生成公钥和私钥
void CRsaEncrypt::CreateKey()
{
memset(&m_PublicKey, 0, sizeof(SRSAKEY));
memset(&m_PrivateKey, 0, sizeof(SRSAKEY));
m_p.Mov(0);
m_q.Mov(0);
m_n.Mov(0);
m_e.Mov(0);
// 生成大素数p、q
m_p.CreatePrime(PRIME_BITS_128);
m_q.CreatePrime(PRIME_BITS_128);
// 计算n(p*q)
m_n.Mov(m_p.Mul(m_q));
// d取常数
m_d.Mov(0x10001);
// 计算e
m_p.m_ulValue[0]--;
m_q.m_ulValue[0]--;
m_p.Mov(m_p.Mul(m_q));
m_e.Mov(m_d.Euc(m_p));
m_q.m_ulValue[0] = 0;
// 设置公钥
m_e.GetValueToStr(m_PublicKey.m_Key_x);
m_n.GetValueToStr(m_PublicKey.m_Key_n);
cout << "cz " << "设置公钥 m_PublicKey.m_Key_x - " << m_PublicKey.m_Key_x << endl;
cout << "cz " << "设置公钥 m_PublicKey.m_Key_n - " << m_PublicKey.m_Key_n << endl;
// 设置私钥
m_d.GetValueToStr(m_PrivateKey.m_Key_x);
strcpy(m_PrivateKey.m_Key_n, m_PublicKey.m_Key_n);
cout << "cz " << "设置私钥 m_PrivateKey.m_Key_x - " << m_PrivateKey.m_Key_x << endl;
cout << "cz " << "设置私钥 m_PrivateKey.m_Key_n - " << m_PrivateKey.m_Key_n << endl;
}
// 加密
bool CRsaEncrypt::Encrypt(unsigned __int64 srcData, char* dstData, SRSAKEY publicKey)
{
m_p.SetValueFromNumber(srcData);
m_n.SetValueFromHexStr(publicKey.m_Key_n, MAX_KEY_LEN);
if (m_p.Cmp(m_n) >= 0)
{
cout << "待加密数据必须小于n" << endl;
return false;
}
CBigInt bigInt_e;
CBigInt bigInt_n;
bigInt_e.SetValueFromHexStr(publicKey.m_Key_x, MAX_KEY_LEN);
bigInt_n.SetValueFromHexStr(publicKey.m_Key_n, MAX_KEY_LEN);
m_q.Mov(m_p.RsaTrans(bigInt_e, bigInt_n));
m_q.GetValueToStr(dstData);
cout << "rsa 加密 dstData = " << dstData << endl;
return true;
}
// 解密
bool CRsaEncrypt::Decrypt(char* srcData, int len, char* dstData, SRSAKEY privateKey)
{
cout << "CRsaEncrypt::Decrypt srcData = " << srcData << endl;
cout << "CRsaEncrypt::Decrypt len = " << len << endl;
//cout << "CRsaEncrypt::Decrypt dstData = " << dstData << endl;
cout << "CRsaEncrypt::Decrypt privateKey.m_Key_n = " << privateKey.m_Key_n << endl;
cout << "CRsaEncrypt::Decrypt privateKey.m_Key_x = " << privateKey.m_Key_x << endl;
/* cz 改 */
// char m_Key_n[MAX_KEY_LEN + 1];
// strcpy( m_Key_n, privateKey.m_Key_n );
/* *** */
m_q.SetValueFromHexStr(srcData, len);
CBigInt bigInt_d;
CBigInt bigInt_n;
bigInt_d.SetValueFromHexStr(privateKey.m_Key_x, 5);
bigInt_n.SetValueFromHexStr( privateKey.m_Key_n, MAX_KEY_LEN );
// bigInt_n.SetValueFromHexStr( m_Key_n, MAX_KEY_LEN );
m_p.Mov(m_q.RsaTrans(bigInt_d, bigInt_n));
m_p.GetValueToStr(dstData, DEC);
cout << "CRsaEncrypt::Decrypt dstData = " << dstData << endl;
return true;
}
stdafx.h
// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//
#pragma once
#include "targetver.h"
#include <cstdio>
#include <tchar.h>
// TODO: 在此处引用程序需要的其他头文件
#include <windows.h>
#include <atlstr.h>
#include <iostream>
#include <string>
#include <cmath>
#include <assert.h>
using namespace std;
#define ASSERT assert
targetver.h
#pragma once
// 以下宏定义要求的最低平台。要求的最低平台
// 是具有运行应用程序所需功能的 Windows、Internet Explorer 等产品的
// 最早版本。通过在指定版本及更低版本的平台上启用所有可用的功能,宏可以
// 正常工作。
// 如果必须要针对低于以下指定版本的平台,请修改下列定义。
// 有关不同平台对应值的最新信息,请参考 MSDN。
#ifndef _WIN32_WINNT // 指定要求的最低平台是 Windows Vista。
#define _WIN32_WINNT 0x0600 // 将此值更改为相应的值,以适用于 Windows 的其他版本。
#endif
#include "stdafx.h"
#include "AesEncrypt.h"
#include "RsaEncrypt.h"
// #define USE_RSA
#define USE_AES_RSA
// 将字符串中的十进制数字转换成unsigned __int64
unsigned __int64 StrToULongLong(char* pStr)
{
unsigned __int64 value = 0;
int len = int(strlen(pStr));
for (int i=0; i<len; i++)
{
value *= 10;
if (pStr[i]>='0'&& pStr[i]<='9')
{
value += pStr[i] - 48;
}
}
return value;
}
// 加密
void AesRsaEncrypt(unsigned char SrcAesKey[8], unsigned char DstAesKey[MAX_KEY_LEN], SRSAKEY publicKey, void* SrcBuf, void* DstBuf, int Size)
{
memset(DstBuf, 0, Size);
unsigned char* pSrcData = (unsigned char*)SrcBuf;
unsigned char* pDstData = (unsigned char*)DstBuf;
unsigned char chBase[BASE_LEN_128];
unsigned char chDst[BASE_LEN_128];
int BlockNum = Size / BASE_LEN_128; // 每16字节为一组进行加密
int RestLen = Size % BASE_LEN_128; // 剩余的字节
ASSERT(RestLen==0 && "加密的字符串长度必须是16的倍数!");
// 用Aes加密数据
CAesEncrypt theAes(16, SrcAesKey);
int index = 0;
for (; index<BlockNum; index++)
{
memset(chBase, 0, sizeof(unsigned char)*BASE_LEN_128);
memset(chDst, 0, sizeof(unsigned char)*BASE_LEN_128);
memcpy(chBase, pSrcData+(index*BASE_LEN_128), BASE_LEN_128);
theAes.Cipher(chBase, chDst);
for (int i=0, k=index*BASE_LEN_128; i<BASE_LEN_128; i++, k++)
{
pDstData[k] = chDst[i];
}
}
if (RestLen > 0)
{
memset(chBase, 0, sizeof(unsigned char)*BASE_LEN_128);
memset(chDst, 0, sizeof(unsigned char)*BASE_LEN_128);
memcpy(chBase, pSrcData+(index*BASE_LEN_128), RestLen);
theAes.Cipher(chBase, chDst);
for (int i=0, k=index*BASE_LEN_128; i<RestLen; i++, k++)
{
pDstData[k] = chDst[i];
}
}
// 用RSA加密SrcAesKey
CRsaEncrypt rsa;
unsigned __int64 dwData = 0;
memcpy(&dwData, (unsigned __int64*)SrcAesKey, 8);
rsa.Encrypt(dwData, (char*)DstAesKey, publicKey);
}
// 解密
void AesRsaDecrypt(unsigned char AesKey[MAX_KEY_LEN], SRSAKEY privateKey, void* SrcBuf, void* DstBuf, int Size)
{
memset(DstBuf, 0, Size);
unsigned char* pSrcData = (unsigned char*)SrcBuf;
unsigned char* pDstData = (unsigned char*)DstBuf;
unsigned char chBase[BASE_LEN_128];
unsigned char chDst[BASE_LEN_128];
int BlockNum = Size / BASE_LEN_128; // 每16字节为一组进行解密
int RestLen = Size % BASE_LEN_128; // 剩余的字节
ASSERT(RestLen==0 && "解密的字符串长度必须是16的倍数!");
// 用RSA解密AesKey
CRsaEncrypt rsa;
char AesKey_De[MAX_KEY_LEN];
//////////////
// SRSAKEY openkey;
// char m_Key_n[MAX_KEY_LEN + 1];
// strcpy( openkey.m_Key_n, privateKey.m_Key_n );
// memcpy( openkey.m_Key_x, privateKey.m_Key_x, (strlen( privateKey.m_Key_x ) + 1) );
/////////////////
cout << "cz 解密前的privateKey = " << privateKey.m_Key_x << " " << privateKey.m_Key_n << endl;
cout << "cz 解密前的AesKey = " << AesKey << endl;
rsa.Decrypt((char*)AesKey, MAX_KEY_LEN, (char*)AesKey_De, privateKey);
cout << "cz 解密后的AesKey = " << AesKey << endl;
cout << "cz 解密后的AesKey_De = " << AesKey_De << endl;
unsigned char chAesKey[8] = "";
unsigned __int64 nKeyDe = StrToULongLong(AesKey_De);
// nKeyDe = 15540725856023089;
cout << "nKeyDe = " << nKeyDe << endl;
memcpy(chAesKey, (unsigned char*)&nKeyDe, 8);
cout << "cz 用于解密的chAesKey = " << chAesKey << endl;
// 用Aes解密数据
CAesEncrypt theAes(16, chAesKey);
int index = 0;
for (; index<BlockNum; index++)
{
memset(chBase, 0, sizeof(unsigned char)*BASE_LEN_128);
memset(chDst, 0, sizeof(unsigned char)*BASE_LEN_128);
memcpy(chBase, pSrcData+(index*BASE_LEN_128), BASE_LEN_128);
theAes.InvCipher(chBase, chDst);
for (int i=0, k=index*BASE_LEN_128; i<BASE_LEN_128; i++, k++)
{
pDstData[k] = chDst[i];
}
}
if (RestLen > 0)
{
memset(chBase, 0, sizeof(unsigned char)*BASE_LEN_128);
memset(chDst, 0, sizeof(unsigned char)*BASE_LEN_128);
memcpy(chBase, pSrcData+(index*BASE_LEN_128), RestLen);
theAes.InvCipher(chBase, chDst);
for (int i=0, k=index*BASE_LEN_128; i<RestLen; i++, k++)
{
pDstData[k] = chDst[i];
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
#ifdef USE_AES_RSA
{
// 字符串加解密
// while (true)
// {
cout << "请输入明文:" << endl;
const int LEN = 32;
unsigned char srcData[LEN+1] = ""; // 数组长度必须是16的倍数,但实际字符数可以不是16的倍数
unsigned char dstData_C[LEN+1] = "";
unsigned char dstData_M[LEN+1] = "";
unsigned char SrcKey[8] = "1234567";
unsigned char DstKey[MAX_KEY_LEN] = "";
cin >> srcData;
SRSAKEY publicKey;
SRSAKEY privateKey;
memset(&publicKey, 0, sizeof(SRSAKEY));
memset(&privateKey, 0, sizeof(SRSAKEY));
CRsaEncrypt rsa;
rsa.CreateKey();
publicKey = rsa.GetPublicKey();
privateKey = rsa.GetPrivateKey();
cout << "cz 加密前 公钥 m_Key_n =" << publicKey.m_Key_n << endl;
cout << "cz 加密前 公钥" << publicKey.m_Key_x << endl;
cout << "cz 加密前 私钥 m_Key_n =" << privateKey.m_Key_n << endl;
cout << "cz 加密前 私钥" << privateKey.m_Key_x << endl;
/* 加密规则的原因 造成不能反过来加密 解密 */
// 加密
AesRsaEncrypt( SrcKey, DstKey, publicKey, srcData, dstData_C, LEN );
// AesRsaEncrypt( SrcKey, DstKey, privateKey, srcData, dstData_C, LEN );
dstData_C[LEN] = 0;
cout << "密文:\n" << dstData_C << endl;
cout << "cz DstKey - " << DstKey << endl;
cout << "cz SrcKey - " << SrcKey << endl;
cout << "cz 加密后 公钥" << publicKey.m_Key_n << endl;
cout << "cz 加密后 公钥" << publicKey.m_Key_x << endl;
cout << "cz 加密后 私钥" << privateKey.m_Key_n << endl;
cout << "cz 加密后 私钥" << privateKey.m_Key_x << endl;
// 解密
AesRsaDecrypt( DstKey, privateKey, dstData_C, dstData_M, LEN );
// AesRsaDecrypt( DstKey, publicKey, dstData_C, dstData_M, LEN );
dstData_M[LEN] = 0;
cout << "明文:\n" << dstData_M << endl << endl;
// 用私钥加密数据 公钥解密数据 不做钥的加密 办不到啊 怎么弄
// 客户端和服务端都保留同一个私钥 公钥加密数据 私钥解密??
cout << endl;
// system("pause");
// }
}
#endif
#ifdef USE_RSA
{
CRsaEncrypt rsa;
cout << "//-------------------------------------------------" << endl;
cout << "// 【RSA加密】" << endl;
cout << "//-------------------------------------------------" << endl;
SRSAKEY publicKey;
SRSAKEY privateKey;
memset(&publicKey, 0, sizeof(SRSAKEY));
memset(&privateKey, 0, sizeof(SRSAKEY));
rsa.CreateKey();
publicKey = rsa.GetPublicKey();
privateKey = rsa.GetPrivateKey();
cout << "公钥:\n";
cout << "e:" << publicKey.m_Key_x << endl;
cout << "n:" << publicKey.m_Key_n << endl << endl;
cout << "私钥:\n";
cout << "d:" << privateKey.m_Key_x << endl;
cout << "n:" << privateKey.m_Key_n << endl << endl;
while (true)
{
cout << "请输入明文:(输入7个字符)" << endl;
char srcData[8] = "1234567";
char dstData_Encrypt[MAX_KEY_LEN+1];
char dstData_Decrypt[MAX_KEY_LEN+1];
cin >> srcData;
unsigned __int64 dwData = 0;
memcpy(&dwData, (unsigned __int64*)srcData, 8);
cout << "明文(unsigned __int64):\n" << dwData << endl;
// 加密
rsa.Encrypt( dwData, dstData_Encrypt, publicKey );
// rsa.Encrypt( dwData, dstData_Encrypt, privateKey );
cout << "密文:\n" << dstData_Encrypt << endl;
// 解密
CRsaEncrypt rsa2;
rsa2.Decrypt( dstData_Encrypt, MAX_KEY_LEN, dstData_Decrypt, privateKey );
// rsa2.Decrypt( dstData_Encrypt, MAX_KEY_LEN, dstData_Decrypt, publicKey );
cout << "明文:\n" << dstData_Decrypt << endl;
unsigned __int64 dwOutData = StrToULongLong(dstData_Decrypt);
char chOutData[8] = "";
memcpy(&chOutData, (char*)&dwOutData, 8);
cout << "输出字符串:\n" << chOutData << endl;
cout << endl;
system("pause");
}
}
#endif
system("pause");
return 0;
}