一、代码
- 封装和nodejs通讯接口:
Encrpt_SM2_new() Decrypt_SM2_new() 加密解密结果可以和nodejs的模块sm-crypto ,加密解密结果可以互相通讯 - c++通讯接口采用
Encrpt_SM2() Decrypt_SM2() - 源码下载地址
int main()
{
int result;
EccPoint p_publicKey ;
uint8_t p_privateKey[NUM_ECC_DIGITS];
uint8_t p_random[NUM_ECC_DIGITS];
result=ecc_make_key(&p_publicKey, p_privateKey, p_random);
int ret = 0;
std::string plain_text = "encryption standard";
unsigned char cplain_text[65535];
plain_text.copy((char*)cplain_text, plain_text.length(), 0);
unsigned char MessageInfo[20] = "junxue";
unsigned char sendbuf[65535] = { 0 };
ret = Encrpt_SM2(MessageInfo,7, sendbuf);
int nSendLen = strlen((const char*)sendbuf);
unsigned char recvbuf[65535] = { 0 };
ret = Decrypt_SM2(sendbuf, 206, recvbuf);
std::string inputStr = "junxue";
unsigned char inputChar[65535];
inputStr.copy((char*)inputChar, inputStr.length(), 0);
int inputLen = inputStr.length();
unsigned char tempbuf[65535] = { 0 };
ret = Encrpt_SM2_new(inputChar, inputLen, tempbuf);
int tempLen = strlen((const char*)tempbuf);
printf("enc result:");
printf("\n");
int i = 0;
for (i = 0; i < tempLen; i++)
{
printf("%02X", tempbuf[i]);
if (1 == (i + 1) % 32 && i!=0)
printf("\n");
}
printf("\n");
std::string encData_str = "6d9e390cc6e44ec5c0ea67f060a8d099a18c34cf6fbada13bbed07bcbb567f737ddfa5672bc3cc34dcbb2a05c5491160a2afc40c0efb4475d08fadc249831c776e56fe1301a8541634056cfd879211b4b70d01f4a8580a578775c600947588e9a8d3ea210d7ff50bc9d2bd8a29c4711295241f76565c474cc077bd045b51e380181bd06726075335bf0b855033dbe554e255664d0cef605d023e15811a7342b0dd1a92bfa40bc5ca1a8d7f9b8ee5cf62e47e1f1b479b62e2af9e7e6578a91604c3146a8824c83b85ac5db8d0f72536";
int encDataLen = encData_str.length();
unsigned char encData_char[65535];
unsigned char decData[65535];
encData_str.copy((char*)encData_char, encDataLen, 0);
;
ret = Decrypt_SM2_new(encData_char, encDataLen, decData);
system("pause");
return 0;
}
|