目录
效果
- 加密字符串:lxw 123abcD!@#$%^&*测试 2024-09-01:12:00
-
- 加密后信息:04B24E2E1B3504CAA543774740DF8166163179960E521B9A723028B1AD96F49F8FFDEA8937F029DB9AE045FAF42CB25141F7676EF84EF16AC5CBA0D9BCFD49B0C08F6AD903E2CE9334F87329370F7736E30271A1755120AFF17F9183D7A0EE7DCCA91FC229B389075179FDA1A877744FA48398407D6A6227CFB88D068DE621D343AF8BA5D8BE70B416C0
-
- 解密后信息:lxw 123abcD!@#$%^&*测试 2024-09-01:12:00
在线校验
地址:https://the-x.cn/cryptography/Sm2.aspx
代码实现参考
https://github.com/yaqiangxue/Test_SM2_encrypt_and_decrypt/tree/master
项目
代码
#include "StdAfx.h"
#include
#include <string>
#include "sm2.h"
using namespace std;
// 将16进制的string字符串,转成unsigned char *
int hexString2UnsignedCharArray(const std::string& hexString, unsigned char * out) {
// 检查十六进制字符串长度是否为偶数
if (hexString.length() % 2 != 0) {
std::cout << "Invalid hex string length." << std::endl;
return -1;
}
// 将十六进制字符串转换为unsigned char数组
int j = 0;
for (size_t i = 0; i < hexString.length(); i += 2) {
std::string byteString = hexString.substr(i, 2);
int decimalValue = std::stoi(byteString, nullptr, 16);
unsigned char byteValue = static_cast
out[j]=byteValue;
j++;
}
return 0;
}
// 将hexarr 转成16进制的字符串 如 0x11 0x22 转了之后是 “1122”
string array2Hex(const unsigned char *arr, size_t len)
{
size_t i;
string res;
char tmp[3];
const char *tab = "0123456789ABCDEF";
res.reserve(len * 2 + 1);
for(i = 0; i < len; ++i) {
tmp[0] = tab[arr[i] >> 4];
tmp[1] = tab[arr[i] & 0xf];
tmp[2] = '\0';
res.append(tmp);
}
return res;
}
int main() {
int error_code; //公钥是加04的。 std::string msg = "lxw 123abcD!@#$%^&*测试 2024-09-01:12:00 "; //私钥 //公钥 unsigned char c1[65], c3[32]; int b=hexString2UnsignedCharArray(priKeyStr,pri_key); b=hexString2UnsignedCharArray(pubKeyStr,pub_key); if ( !(c2 = (unsigned char *)malloc(msg_len)) ) //加密 std::string result=c1str+c2str+c3str; //解密 std::string plainTextStr((char*)plaintext); free(plaintext); getchar();
//生成密钥对
SM2_KEY_PAIR key_pair;
if ( error_code = sm2_create_key_pair(&key_pair) )
{
printf("Create SM2 key pair failed!\n");
return (-1);
}
std::string priKeyStr2=array2Hex(key_pair.pri_key,32);
std::string pubKeyStr2=array2Hex(key_pair.pub_key,65);
std::cout<<"pubKeyStr:"<
std::string pubKeyStr = "04FDFB7C93565AB39E1D8178429632EEC914F6A347AE9A0CE9B201FFAEA81A80CC4D81036191209B21CDBAD8A4BCD5C9A776FEDB771D6D2D8DAC0F1E5941C0F63C";
std::string priKeyStr = "832B9C649C63B376DBD1D858C4D1B804CCFF6F7B6B588A9F30A54AF821F80E86";
std::cout<<"加密字符串:"<
unsigned char pri_key[32] = {0};
unsigned long pri_key_len = 32;
unsigned char pub_key[65] = {0};
unsigned long pub_key_len = 65;
unsigned long c1Len = 65;
unsigned long c3Len = 32;
unsigned char *c2, *plaintext;
if(b != 0)
{
printf("转换priKeyStr失败\n");
}
if(b != 0)
{
printf("转换pubKeyStr失败\n");
}
{
printf("Memory allocation failed!\n");
return ALLOCATION_MEMORY_FAIL;
}
if ( error_code = sm2_encrypt((unsigned char *)msg.c_str(),
msg_len,
pub_key,
c1,
c3,
c2) )
{
printf("Create SM2 ciphertext by using input defined in standard failed!\n");
free(c2);
return error_code;
}
std::string c1str=array2Hex(c1,c1Len);
std::string c2str=array2Hex(c2,msg_len);
std::string c3str=array2Hex(c3,c3Len);
std::cout<<"加密后信息:"<
if ( !(plaintext = (unsigned char *)malloc(c2str.length())) )
{
printf("Memory allocation failed!\n");
return ALLOCATION_MEMORY_FAIL;
}
if ( error_code = sm2_decrypt(c1,
c3,
c2,
msg_len,
pri_key,
plaintext) )
{
printf("Decrypt SM2 ciphertext by using private key defined in standard failed!\n");
}
//添加空终止符
plaintext[msg_len] = '\0';
std::cout<<"解密后信息:"<
free(c2);
return 0;
}下载


评论记录:
回复评论: