一、RSA算法简介
void CPage4::OnButtonPubDecrypt()
{
// TODO: Add your control notification handler code here
unsigned char rsaN[1024] = {0};
unsigned char rsaD[1024] = {0};
unsigned char rsaE[1024] = {0};
unsigned char public_e[8] = {0};
unsigned char publickey_n[1024] = {0};
unsigned char privatekey_d[1024] = {0};
unsigned char inputdata[1024] = {0};
unsigned char output[1024] = {0};
int e_len = 0;
int n_len = 0;
int d_len = 0;
int inputlen = 0;
UpdateData(TRUE);
m_public_e.Remove(' ');
m_publickey_n.Remove(' ');
m_privatekey_d.Remove(' ');
m_inputdata.Remove(' ');
e_len = m_public_e.GetLength()/2;
n_len = m_publickey_n.GetLength()/2;
d_len = m_privatekey_d.GetLength()/2;
inputlen = m_inputdata.GetLength()/2;
StrToHex(m_public_e,public_e,e_len);
StrToHex(m_publickey_n,publickey_n,n_len);
StrToHex(m_privatekey_d,privatekey_d,d_len);
StrToHex(m_inputdata,inputdata,inputlen);
memcpy(rsaN,publickey_n,n_len);
memcpy(rsaD,privatekey_d,d_len);
memcpy(rsaE,public_e,e_len);
RSA* pRsa = RSA_new();
pRsa->n = BN_bin2bn(rsaN, n_len, NULL);
pRsa->d = BN_bin2bn(rsaD, d_len, NULL);
pRsa->e = BN_bin2bn(rsaE, e_len, NULL);
inputlen=RSA_size(pRsa);
int ret = RSA_public_encrypt(inputlen,inputdata,output, pRsa, RSA_NO_PADDING);
if (ret < 0)
{
MessageBox("公钥加密失败,请检查输入数据是否正确!");
RSA_free(pRsa);
return;
}
RSA_free(pRsa);
HexToStr(output,ret,m_outputdata);
UpdateData(FALSE);
}
void CPage4::OnButtonPriDecrypt()
{
// TODO: Add your control notification handler code here
unsigned char rsaN[1024] = {0};
unsigned char rsaD[1024] = {0};
unsigned char rsaE[1024] = {0};
unsigned char public_e[8] = {0};
unsigned char publickey_n[1024] = {0};
unsigned char privatekey_d[1024] = {0};
unsigned char inputdata[1024] = {0};
unsigned char output[1024] = {0};
int e_len = 0;
int n_len = 0;
int d_len = 0;
int inputlen = 0;
UpdateData(TRUE);
m_public_e.Remove(' ');
m_publickey_n.Remove(' ');
m_privatekey_d.Remove(' ');
m_inputdata.Remove(' ');
e_len = m_public_e.GetLength()/2;
n_len = m_publickey_n.GetLength()/2;
d_len = m_privatekey_d.GetLength()/2;
inputlen = m_inputdata.GetLength()/2;
StrToHex(m_public_e,public_e,e_len);
StrToHex(m_publickey_n,publickey_n,n_len);
StrToHex(m_privatekey_d,privatekey_d,d_len);
StrToHex(m_inputdata,inputdata,inputlen);
memcpy(rsaN,publickey_n,n_len);
memcpy(rsaD,privatekey_d,d_len);
memcpy(rsaE,public_e,e_len);
RSA* pRsa = RSA_new();
pRsa->n = BN_bin2bn(rsaN, n_len, NULL);
pRsa->d = BN_bin2bn(rsaD, d_len, NULL);
pRsa->e = BN_bin2bn(rsaE, e_len, NULL);
//inputlen=RSA_size(pRsa)-11;
int ret = RSA_private_decrypt(inputlen,inputdata,output, pRsa, RSA_NO_PADDING);
if (ret < 0)
{
MessageBox("私钥解密失败,请检查输入数据是否正确!");
RSA_free(pRsa);
return;
}
RSA_free(pRsa);
HexToStr(output,ret,m_outputdata);
UpdateData(FALSE);
}
void CPage4::OnButtonEncrypt()
{
// TODO: Add your control notification handler code here
unsigned char rsaN[1024] = {0};
unsigned char rsaD[1024] = {0};
unsigned char rsaE[1024] = {0};
unsigned char public_e[8] = {0};
unsigned char publickey_n[1024] = {0};
unsigned char privatekey_d[1024] = {0};
unsigned char inputdata[1024] = {0};
unsigned char output[1024] = {0};
int e_len = 0;
int n_len = 0;
int d_len = 0;
int inputlen = 0;
UpdateData(TRUE);
m_public_e.Remove(' ');
m_publickey_n.Remove(' ');
m_privatekey_d.Remove(' ');
m_inputdata.Remove(' ');
e_len = m_public_e.GetLength()/2;
n_len = m_publickey_n.GetLength()/2;
d_len = m_privatekey_d.GetLength()/2;
inputlen = m_inputdata.GetLength()/2;
StrToHex(m_public_e,public_e,e_len);
StrToHex(m_publickey_n,publickey_n,n_len);
StrToHex(m_privatekey_d,privatekey_d,d_len);
StrToHex(m_inputdata,inputdata,inputlen);
memcpy(rsaN,publickey_n,n_len);
memcpy(rsaD,privatekey_d,d_len);
memcpy(rsaE,public_e,e_len);
RSA* pRsa = RSA_new();
pRsa->n = BN_bin2bn(rsaN, n_len, NULL);
pRsa->d = BN_bin2bn(rsaD, d_len, NULL);
pRsa->e = BN_bin2bn(rsaE, e_len, NULL);
inputlen=RSA_size(pRsa);
int ret = RSA_private_encrypt(inputlen,inputdata,output, pRsa, RSA_NO_PADDING);
if (ret < 0)
{
MessageBox("私钥加密失败,请检查输入数据是否正确!");
RSA_free(pRsa);
return;
}
RSA_free(pRsa);
HexToStr(output,ret,m_outputdata);
UpdateData(FALSE);
}
void CPage4::OnButtonDecrypt()
{
// TODO: Add your control notification handler code here
unsigned char rsaN[1024] = {0};
unsigned char rsaD[1024] = {0};
unsigned char rsaE[1024] = {0};
unsigned char public_e[8] = {0};
unsigned char publickey_n[1024] = {0};
unsigned char privatekey_d[1024] = {0};
unsigned char inputdata[1024] = {0};
unsigned char output[1024] = {0};
int e_len = 0;
int n_len = 0;
int d_len = 0;
int inputlen = 0;
UpdateData(TRUE);
m_public_e.Remove(' ');
m_publickey_n.Remove(' ');
m_privatekey_d.Remove(' ');
m_inputdata.Remove(' ');
e_len = m_public_e.GetLength()/2;
n_len = m_publickey_n.GetLength()/2;
d_len = m_privatekey_d.GetLength()/2;
inputlen = m_inputdata.GetLength()/2;
StrToHex(m_public_e,public_e,e_len);
StrToHex(m_publickey_n,publickey_n,n_len);
StrToHex(m_privatekey_d,privatekey_d,d_len);
StrToHex(m_inputdata,inputdata,inputlen);
memcpy(rsaN,publickey_n,n_len);
memcpy(rsaD,privatekey_d,d_len);
memcpy(rsaE,public_e,e_len);
RSA* pRsa = RSA_new();
pRsa->n = BN_bin2bn(rsaN, n_len, NULL);
pRsa->d = BN_bin2bn(rsaD, d_len, NULL);
pRsa->e = BN_bin2bn(rsaE, e_len, NULL);
//inputlen=RSA_size(pRsa)-11;
int ret = RSA_public_decrypt(inputlen,inputdata,output, pRsa, RSA_NO_PADDING);
if (ret < 0)
{
MessageBox("公钥解密失败,请检查输入数据是否正确!");
RSA_free(pRsa);
return;
}
RSA_free(pRsa);
HexToStr(output,ret,m_outputdata);
UpdateData(FALSE);
}
void CPage4::OnButtonRsa()
{
// TODO: Add your control notification handler code here
RSA* key;
unsigned char rsa_n[1024];
unsigned char rsa_d[1024];
char keylen[10];
int len;
UpdateData(TRUE);
strcpy(keylen,m_keylen);
len = atoi(keylen);
key = RSA_new();
key = RSA_generate_key(len,0x10001,NULL,NULL);
BN_bn2bin(key->n,rsa_n ); // 保存公钥
BN_bn2bin(key->d,rsa_d ); // 保存私钥
RSA_free(key);
HexToStr(rsa_n,len/8,m_publickey_n);
HexToStr(rsa_d,len/8,m_privatekey_d);
UpdateData(FALSE);
}