当前位置:   article > 正文

国密算法学习笔记_crypto加密算法库支持国密算法吗

crypto加密算法库支持国密算法吗

国密算法学习笔记

国密算法简介

国密算法是由中国密码学专家自主设计并发布的一系列密码算法,它们包括SM1、SM2、SM3、SM4等。

SM1是一种对称加密算法,采用128位密钥长度,支持分组长度为64位或者128位,具有较高的安全性和性能。

SM2是一种非对称加密算法,基于椭圆曲线密码体制,具有与RSA算法相当的安全性,同时具有更高的运算速度和更短的密钥长度。SM2算法主要用于数字签名、密钥交换、加密和解密等方面的应用。

SM3是一种密码杂凑算法,用于实现消息摘要的功能,具有较高的安全性和性能。

SM4是一种对称加密算法,采用128位密钥长度,支持分组长度为128位,具有高效、安全、易用等特点,已成为国家标准。

国密算法主要应用于政务、金融、电信、移动支付等领域,以保障重要数据的安全性。同时,国密算法也已经逐渐被国际社会所认可和采用。

国密算法C++可以使用的第三方库

GMSSL(国密SSL):是一个由OSCCA(OpenSSL Software Foundation in China)开发的开源软件包,它实现了SM2,SM3,SM4等多种国密算法,并提供了与OpenSSL兼容的API接口,可以方便地在C++中使用。

Botan:是一个跨平台的密码学库,支持多种密码算法,包括国密算法SM2,SM3和SM4等。它提供了简单易用的API接口,可以方便地在C++中使用。

Crypto++:是一个流行的密码学库,支持多种密码算法,包括国密算法SM2,SM3和SM4等。它提供了丰富的API接口,可以方便地在C++中使用。

mbed TLS:是一个轻量级的加密库,支持多种密码算法,包括国密算法SM2,SM3和SM4等。它提供了简单易用的API接口,可以方便地在C++中使用。

以上是一些常用的C++国密第三方库,你可以根据自己的需求选择其中一个来使用。请注意,在使用国密算法时,需要遵循国家相关的法律法规。

mbedTLS 官方版本不支持国密

mbedTLS的官方地址: https://tls.mbed.org/
mbedTLS的官方文档地址:https://mbed-tls.readthedocs.io/en/latest/
GitHub仓库地址: https://github.com/ARMmbed/mbedtls

mbedTLS(前身为PolarSSL)是一个开源的轻量级加密和SSL/TLS协议库,它提供了一些常用的加密算法和协议实现,包括AES、SM4、SHA-1、SHA-256、RSA、ECDH、TLS、DTLS等。它被广泛用于物联网、安全通信、云服务等领域的安全通信。

mbedTLS库的特点如下:

轻量级:mbedTLS库的代码量很小,体积较小,易于嵌入到各种设备中。
可移植性:mbedTLS库可以运行在各种平台和操作系统上,包括嵌入式系统和桌面系统。
安全性:mbedTLS库提供了一些流行的加密算法和协议的实现,并且它经过了广泛的安全测试和评估,可靠性很高。
简单易用:mbedTLS库提供了易于使用的API,方便开发人员使用和集成到应用程序中。
mbedTLS库可以使用C语言编写,并且是开源的,可在GNU Lesser General Public License(LGPL)下使用。

# root @ ubuntu in /opt/hrs-server/java on git:v2.01.05 o [1:50:44] 
$ apt-cache search mbed |grep dev | grep mbedtls
libmbedtls-dev - lightweight crypto and SSL/TLS library - development files
# root @ ubuntu in /opt/hrs-server/java on git:v2.01.05 o [1:52:47] 
$ apt-cache show libmbedtls-dev
Package: libmbedtls-dev
Architecture: amd64
Version: 2.8.0-1
Multi-Arch: same
Priority: optional
Section: universe/libdevel
Source: mbedtls
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: James Cowgill <jcowgill@debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 2116
Depends: libmbedcrypto1 (= 2.8.0-1), libmbedtls10 (= 2.8.0-1), libmbedx509-0 (= 2.8.0-1)
Suggests: libmbedtls-doc
Filename: pool/universe/m/mbedtls/libmbedtls-dev_2.8.0-1_amd64.deb
Size: 381764
MD5sum: 24c030f986eba86580f29207ad182f01
SHA1: b274b40b51e98b84908eb967b90ab14388fc3447
SHA256: 2e1f16130648644d131d9fe79022a24a66bd1b1f79ff61aebffe6f0d5b144957
Homepage: https://tls.mbed.org/
Description-en: lightweight crypto and SSL/TLS library - development files
 mbed TLS (formerly known as PolarSSL) is a lean open source crypto library for
 providing SSL and TLS support in your programs. It offers an intuitive API and
 documented header files, so you can actually understand what the code does.
 It features:
  - Symmetric algorithms, like AES, Blowfish, Triple-DES, DES, ARC4, Camellia
    and XTEA
  - Hash algorithms, like SHA-1, SHA-2, RIPEMD-160 and MD5
  - Entropy pool and random generators, like CTR-DRBG and HMAC-DRBG
  - Public key algorithms, like RSA, Elliptic Curves, Diffie-Hellman, ECDSA
    and ECDH
  - TLS 1.0, 1.1 and 1.2
  - Abstraction layers for ciphers, hashes, public key operations, platform
    abstraction and threading
 .
 This package contains the header files and static libraries for mbed TLS.
Description-md5: 40f9a71b90e23ccf34a190093807138a

# root @ ubuntu in /opt/hrs-server/java on git:v2.01.05 o [1:50:53] 
$ apt-cache depends libmbedtls-dev
libmbedtls-dev
  Depends: libmbedcrypto1
  Depends: libmbedtls10
  Depends: libmbedx509-0
  Suggests: libmbedtls-doc
# root @ ubuntu in /opt/hrs-server/java on git:v2.01.05 o [2:01:22] C:1
$ dpkg -L libmbedtls-dev |grep sm4.h
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

在ubuntu18.04的apt源中,有这个库可以下载使用,但是没有sm4.h文件,估计是版本太低。

SM4加密示例

#include <stdio.h>
#include "mbedtls/config.h"
#include "mbedtls/platform.h"
#include "mbedtls/aes.h"
#include "mbedtls/sm4.h"

int main(void)
{
    int ret;
    mbedtls_sm4_context ctx;
    unsigned char key[16] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10};
    unsigned char iv[16] = {0};
    unsigned char input[16] = {0x68, 0x75, 0x61, 0x6e, 0x67, 0x6a, 0x69, 0x65, 0x6c, 0x69, 0x61, 0x6e, 0x67, 0x79, 0x61, 0x6e};
    unsigned char output[16] = {0};
    
    // 初始化SM4上下文
    mbedtls_sm4_init(&ctx);
    
    // 设置SM4解密密钥
    ret = mbedtls_sm4_setkey_dec(&ctx, key, 128);
    if (ret != 0) {
        printf("mbedtls_sm4_setkey_dec failed with error code: %d\n", ret);
        goto exit;
    }
    
    // 使用SM4 CBC模式进行解密
    ret = mbedtls_sm4_crypt_cbc(&ctx, MBEDTLS_SM4_DECRYPT, sizeof(input), iv, input, output);
    if (ret != 0) {
        printf("mbedtls_sm4_crypt_cbc failed with error code: %d\n", ret);
        goto exit;
    }
    
    // 输出解密结果
    printf("Decrypt output: ");
    for (int i = 0; i < sizeof(output); i++) {
        printf("%02x", output[i]);
    }
    printf("\n");
    
exit:
    // 清除SM4上下文
    mbedtls_sm4_free(&ctx);
    
    return ret;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

SM4解密示例

#include <stdio.h>
#include "mbedtls/config.h"
#include "mbedtls/platform.h"
#include "mbedtls/aes.h"
#include "mbedtls/sm4.h"

int main(void)
{
    int ret;
    mbedtls_sm4_context ctx;
    unsigned char key[16] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10};
    unsigned char iv[16] = {0};
    unsigned char input[16] = {0x68, 0x75, 0x61, 0x6e, 0x67, 0x6a, 0x69, 0x65, 0x6c, 0x69, 0x61, 0x6e, 0x67, 0x79, 0x61, 0x6e};
    unsigned char output[16] = {0};
    
    // 初始化SM4上下文
    mbedtls_sm4_init(&ctx);
    
    // 设置SM4解密密钥
    ret = mbedtls_sm4_setkey_dec(&ctx, key, 128);
    if (ret != 0) {
        printf("mbedtls_sm4_setkey_dec failed with error code: %d\n", ret);
        goto exit;
    }
    
    // 使用SM4 CBC模式进行解密
    ret = mbedtls_sm4_crypt_cbc(&ctx, MBEDTLS_SM4_DECRYPT, sizeof(input), iv, input, output);
    if (ret != 0) {
        printf("mbedtls_sm4_crypt_cbc failed with error code: %d\n", ret);
        goto exit;
    }
    
    // 输出解密结果
    printf("Decrypt output: ");
    for (int i = 0; i < sizeof(output); i++) {
        printf("%02x", output[i]);
    }
    printf("\n");
    
exit:
    // 清除SM4上下文
    mbedtls_sm4_free(&ctx);
    
    return ret;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/139599
推荐阅读
相关标签
  

闽ICP备14008679号