立山 发表于 2026-1-29 17:47:57

C++国密SM2算法加解密的利用

https://dis.qidao123.com/imgproxy/aHR0cHM6Ly9pLWJsb2cuY3NkbmltZy5jbi9ibG9nX21pZ3JhdGUvNmY2ZWQ2NTQ3MjNlMTVlYjFkNDZhNGY5N2ZiMzI0MDQuanBlZw==
目次
结果
在线校验
代码实现参考
项目
代码
下载
结果

https://dis.qidao123.com/imgproxy/aHR0cHM6Ly9pLWJsb2cuY3NkbmltZy5jbi9kaXJlY3QvNGI5MjI1MjgzZmY4NGQxZGIwZDVmMWUzNDJmOWU5ZGUucG5n
加密字符串:lxw 123abcD!@#$%^&*测试 2024-09-01:12:00

加密后信息:04B24E2E1B3504CAA543774740DF8166163179960E521B9A723028B1AD96F49F8FFDEA8937F029DB9AE045FAF42CB25141F7676EF84EF16AC5CBA0D9BCFD49B0C08F6AD903E2CE9334F87329370F7736E30271A1755120AFF17F9183D7A0EE7DCCA91FC229B389075179FDA1A877744FA48398407D6A6227CFB88D068DE621D343AF8BA5D8BE70B416C0

解密后信息:lxw 123abcD!@#$%^&*测试 2024-09-01:12:00 在线校验

地点:https://the-x.cn/cryptography/Sm2.aspx
https://dis.qidao123.com/imgproxy/aHR0cHM6Ly9pLWJsb2cuY3NkbmltZy5jbi9kaXJlY3QvZmQ0NjQ2N2IxYTlkNGQ0NDhlMzMwZDIxMDY2ZTE1NTMucG5n
代码实现参考

https://github.com/yaqiangxue/Test_SM2_encrypt_and_decrypt/tree/master
项目

https://dis.qidao123.com/imgproxy/aHR0cHM6Ly9pLWJsb2cuY3NkbmltZy5jbi9kaXJlY3QvNGUwNjZiNGJmNWQyNGRjOTliZGY5ZTdjMGY2YjRlNGEucG5n
代码

#include "StdAfx.h"
#include <iostream>
#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<unsigned char>(decimalValue);
        out=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;
    const char *tab = "0123456789ABCDEF";
    res.reserve(len * 2 + 1);
    for(i = 0; i < len; ++i) {
        tmp = tab >> 4];
        tmp = tab & 0xf];
        tmp = '\0';
        res.append(tmp);
    }
    return res;
}
int main() {
    int error_code;
    //天生密钥对
    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:"<<pubKeyStr2<<std::endl;
    std::cout<<"priKeyStr:"<<priKeyStr2<<std::endl<<std::endl;
    //公钥是加04的。
    std::string pubKeyStr = "04FDFB7C93565AB39E1D8178429632EEC914F6A347AE9A0CE9B201FFAEA81A80CC4D81036191209B21CDBAD8A4BCD5C9A776FEDB771D6D2D8DAC0F1E5941C0F63C";
    std::string priKeyStr = "832B9C649C63B376DBD1D858C4D1B804CCFF6F7B6B588A9F30A54AF821F80E86";
    std::string msg = "lxw 123abcD!@#$%^&*测试 2024-09-01:12:00 ";
    std::cout<<"加密字符串:"<<msg<<std::endl<<std::endl;
    int msg_len = msg.length();
    //私钥
    unsigned char pri_key = {0};
    unsigned long pri_key_len = 32;
    //公钥
    unsigned char pub_key = {0}; 
    unsigned long pub_key_len = 65;
    unsigned char c1, c3;
    unsigned long c1Len = 65;
    unsigned long c3Len = 32;
    unsigned char *c2, *plaintext;
    int b=hexString2UnsignedCharArray(priKeyStr,pri_key);
    if(b != 0)
    {
        printf("转换priKeyStr失败\n");
    }
    b=hexString2UnsignedCharArray(pubKeyStr,pub_key);
    if(b != 0)
    {
        printf("转换pubKeyStr失败\n");
    }
    if ( !(c2 = (unsigned char *)malloc(msg_len)) )
    {
        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::string result=c1str+c2str+c3str;
    std::cout<<"加密后信息:"<<result<<std::endl<<std::endl;
    //解密
    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 = '\0';
    std::string plainTextStr((char*)plaintext);
    std::cout<<"解密后信息:"<<plainTextStr<<std::endl;
    free(plaintext);
    free(c2);
    getchar();
    return 0;
}
#include "StdAfx.h"
#include <iostream>
#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<unsigned char>(decimalValue);
                out=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;
        const char *tab = "0123456789ABCDEF";
        res.reserve(len * 2 + 1);
        for(i = 0; i < len; ++i) {
                tmp = tab >> 4];
                tmp = tab & 0xf];
                tmp = '\0';
                res.append(tmp);
        }
        return res;
}

int main() {

        int error_code;
        //生成密钥对
        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:"<<pubKeyStr2<<std::endl;
        std::cout<<"priKeyStr:"<<priKeyStr2<<std::endl<<std::endl;

        //公钥是加04的。
        std::string pubKeyStr = "04FDFB7C93565AB39E1D8178429632EEC914F6A347AE9A0CE9B201FFAEA81A80CC4D81036191209B21CDBAD8A4BCD5C9A776FEDB771D6D2D8DAC0F1E5941C0F63C";
        std::string priKeyStr = "832B9C649C63B376DBD1D858C4D1B804CCFF6F7B6B588A9F30A54AF821F80E86";

        std::string msg = "lxw 123abcD!@#$%^&*测试 2024-09-01:12:00 ";
        std::cout<<"加密字符串:"<<msg<<std::endl<<std::endl;
        int msg_len = msg.length();

        //私钥
        unsigned char pri_key = {0};
        unsigned long pri_key_len = 32;

        //公钥
        unsigned char pub_key = {0};
        unsigned long pub_key_len = 65;

        unsigned char c1, c3;
        unsigned long c1Len = 65;
        unsigned long c3Len = 32;
        unsigned char *c2, *plaintext;

        int b=hexString2UnsignedCharArray(priKeyStr,pri_key);
        if(b != 0)
        {
                printf("转换priKeyStr失败\n");
        }

        b=hexString2UnsignedCharArray(pubKeyStr,pub_key);
        if(b != 0)
        {
                printf("转换pubKeyStr失败\n");
        }

        if ( !(c2 = (unsigned char *)malloc(msg_len)) )
        {
                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::string result=c1str+c2str+c3str;
        std::cout<<"加密后信息:"<<result<<std::endl<<std::endl;

        //解密
        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 = '\0';

        std::string plainTextStr((char*)plaintext);
        std::cout<<"解密后信息:"<<plainTextStr<<std::endl;

        free(plaintext);
        free(c2);

        getchar();
        return 0;
} 下载

源码下载

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!qidao123.com:ToB企服之家,中国第一个企服评测及软件市场,开放入驻,技术点评得现金
页: [1]
查看完整版本: C++国密SM2算法加解密的利用