首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

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

  • 25-02-19 03:41
  • 2129
  • 7215
blog.csdn.net

目录

效果

在线校验

代码实现参考

项目

代码

下载


效果

  1. 加密字符串:lxw 123abcD!@#$%^&*测试 2024-09-01:12:00
  2. 加密后信息:04B24E2E1B3504CAA543774740DF8166163179960E521B9A723028B1AD96F49F8FFDEA8937F029DB9AE045FAF42CB25141F7676EF84EF16AC5CBA0D9BCFD49B0C08F6AD903E2CE9334F87329370F7736E30271A1755120AFF17F9183D7A0EE7DCCA91FC229B389075179FDA1A877744FA48398407D6A6227CFB88D068DE621D343AF8BA5D8BE70B416C0
  3. 解密后信息: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(decimalValue);
        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;
    //生成密钥对
    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::cout<<"priKeyStr:"<

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

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

    //私钥
    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 char c1[65], c3[32];
    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<<"加密后信息:"<

    //解密
    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::string plainTextStr((char*)plaintext);
    std::cout<<"解密后信息:"<

    free(plaintext);
    free(c2);

    getchar();
    return 0;
}

  1. #include "StdAfx.h"
  2. #include <iostream>
  3. #include <string>
  4. #include "sm2.h"
  5. using namespace std;
  6. // 将16进制的string字符串,转成unsigned char *
  7. int hexString2UnsignedCharArray(const std::string& hexString, unsigned char * out) {
  8. // 检查十六进制字符串长度是否为偶数
  9. if (hexString.length() % 2 != 0) {
  10. std::cout << "Invalid hex string length." << std::endl;
  11. return -1;
  12. }
  13. // 将十六进制字符串转换为unsigned char数组
  14. int j = 0;
  15. for (size_t i = 0; i < hexString.length(); i += 2) {
  16. std::string byteString = hexString.substr(i, 2);
  17. int decimalValue = std::stoi(byteString, nullptr, 16);
  18. unsigned char byteValue = static_cast<unsigned char>(decimalValue);
  19. out[j]=byteValue;
  20. j++;
  21. }
  22. return 0;
  23. }
  24. // 将hexarr 转成16进制的字符串 如 0x11 0x22 转了之后是 “1122”
  25. string array2Hex(const unsigned char *arr, size_t len)
  26. {
  27. size_t i;
  28. string res;
  29. char tmp[3];
  30. const char *tab = "0123456789ABCDEF";
  31. res.reserve(len * 2 + 1);
  32. for(i = 0; i < len; ++i) {
  33. tmp[0] = tab[arr[i] >> 4];
  34. tmp[1] = tab[arr[i] & 0xf];
  35. tmp[2] = '\0';
  36. res.append(tmp);
  37. }
  38. return res;
  39. }
  40. int main() {
  41. int error_code;
  42. //生成密钥对
  43. SM2_KEY_PAIR key_pair;
  44. if ( error_code = sm2_create_key_pair(&key_pair) )
  45. {
  46. printf("Create SM2 key pair failed!\n");
  47. return (-1);
  48. }
  49. std::string priKeyStr2=array2Hex(key_pair.pri_key,32);
  50. std::string pubKeyStr2=array2Hex(key_pair.pub_key,65);
  51. std::cout<<"pubKeyStr:"<<pubKeyStr2<<std::endl;
  52. std::cout<<"priKeyStr:"<<priKeyStr2<<std::endl<<std::endl;
  53. //公钥是加04的。
  54. std::string pubKeyStr = "04FDFB7C93565AB39E1D8178429632EEC914F6A347AE9A0CE9B201FFAEA81A80CC4D81036191209B21CDBAD8A4BCD5C9A776FEDB771D6D2D8DAC0F1E5941C0F63C";
  55. std::string priKeyStr = "832B9C649C63B376DBD1D858C4D1B804CCFF6F7B6B588A9F30A54AF821F80E86";
  56. std::string msg = "lxw 123abcD!@#$%^&*测试 2024-09-01:12:00 ";
  57. std::cout<<"加密字符串:"<<msg<<std::endl<<std::endl;
  58. int msg_len = msg.length();
  59. //私钥
  60. unsigned char pri_key[32] = {0};
  61. unsigned long pri_key_len = 32;
  62. //公钥
  63. unsigned char pub_key[65] = {0};
  64. unsigned long pub_key_len = 65;
  65. unsigned char c1[65], c3[32];
  66. unsigned long c1Len = 65;
  67. unsigned long c3Len = 32;
  68. unsigned char *c2, *plaintext;
  69. int b=hexString2UnsignedCharArray(priKeyStr,pri_key);
  70. if(b != 0)
  71. {
  72. printf("转换priKeyStr失败\n");
  73. }
  74. b=hexString2UnsignedCharArray(pubKeyStr,pub_key);
  75. if(b != 0)
  76. {
  77. printf("转换pubKeyStr失败\n");
  78. }
  79. if ( !(c2 = (unsigned char *)malloc(msg_len)) )
  80. {
  81. printf("Memory allocation failed!\n");
  82. return ALLOCATION_MEMORY_FAIL;
  83. }
  84. //加密
  85. if ( error_code = sm2_encrypt((unsigned char *)msg.c_str(),
  86. msg_len,
  87. pub_key,
  88. c1,
  89. c3,
  90. c2) )
  91. {
  92. printf("Create SM2 ciphertext by using input defined in standard failed!\n");
  93. free(c2);
  94. return error_code;
  95. }
  96. std::string c1str=array2Hex(c1,c1Len);
  97. std::string c2str=array2Hex(c2,msg_len);
  98. std::string c3str=array2Hex(c3,c3Len);
  99. std::string result=c1str+c2str+c3str;
  100. std::cout<<"加密后信息:"<<result<<std::endl<<std::endl;
  101. //解密
  102. if ( !(plaintext = (unsigned char *)malloc(c2str.length())) )
  103. {
  104. printf("Memory allocation failed!\n");
  105. return ALLOCATION_MEMORY_FAIL;
  106. }
  107. if ( error_code = sm2_decrypt(c1,
  108. c3,
  109. c2,
  110. msg_len,
  111. pri_key,
  112. plaintext) )
  113. {
  114. printf("Decrypt SM2 ciphertext by using private key defined in standard failed!\n");
  115. }
  116. //添加空终止符
  117. plaintext[msg_len] = '\0';
  118. std::string plainTextStr((char*)plaintext);
  119. std::cout<<"解密后信息:"<<plainTextStr<<std::endl;
  120. free(plaintext);
  121. free(c2);
  122. getchar();
  123. return 0;
  124. }

下载

源码下载

天天代码码天天
微信公众号
.NET 人工智能实践
注:本文转载自blog.csdn.net的天天代码码天天的文章"https://lw112190.blog.csdn.net/article/details/141786008"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top