openssl gmssl sm3 sm4

SM3 是国密摘要算法

openssl 中使用方法如下

$echo "httpfsfgsfgfgsgsfs" > test.dat 
$openssl dgst -SM3 test.dat 
$SM3(test.dat)= ebf5e0f6e101a9320fab11365fb23f69a5c65968302635a7bdf2a0392f5463a5

gmssl 中使用方法如下

$ gmssl sm3 <yourfile>
SM3(yourfile)= 66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0
#将sm3 保存成文件
$ gmssl sm3 -binary -out filesm3 <yourfile>

SM4 是分组对称加密算法

openssl 中使用方法如下

$openssl sm4 -in test.dat -out encrypt.txt
enter sm4-cbc encryption password:******
Verifying - enter sm4-cbc encryption password:*****

$openssl sm4 -d -in encrypt.txt -out plain.txt
enter sms4-cbc decryption password:********

gmssl 中使用方法如下

$ echo hello | gmssl enc -sms4-cbc > ciphertext.bin
enter sms4-cbc encryption password:********
Verifying - enter sms4-cbc encryption password:********

$ cat cipehrtext.bin | gmssl enc -sms4-cbc -d
enter sms4-cbc decryption password:********
hello
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
gmssl是一个开源的密码学库,提供了多种密码算法的实现,包括SM2算法。SM2是中国自主设计的一种非对称加密算法,用于数字签名和密钥交换。 在C++中使用gmssl库进行SM2算法的实现,可以按照以下步骤进行: 1. 首先,需要下载并安装gmssl库。可以从gmssl官方网站或者GitHub上获取源代码,并按照官方提供的安装指南进行编译和安装。 2. 在C++代码中引入gmssl库的头文件,例如: ```cpp #include <openssl/evp.h> #include <openssl/sm2.h> ``` 3. 初始化gmssl库,可以使用以下代码: ```cpp OpenSSL_add_all_algorithms(); ``` 4. 生成SM2密钥对,可以使用以下代码: ```cpp EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL); EVP_PKEY_keygen_init(ctx); EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, NID_sm2p256v1); EVP_PKEY_keygen(ctx, &pkey); ``` 5. 使用SM2进行数字签名,可以使用以下代码: ```cpp EVP_MD_CTX *md_ctx = EVP_MD_CTX_new(); EVP_DigestSignInit(md_ctx, NULL, EVP_sm3(), NULL, pkey); EVP_DigestSignUpdate(md_ctx, data, data_len); EVP_DigestSignFinal(md_ctx, signature, &signature_len); ``` 6. 使用SM2进行密钥交换,可以使用以下代码: ```cpp EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(pkey, NULL); EVP_PKEY_derive_init(ctx); EVP_PKEY_derive_set_peer(ctx, peer_key); EVP_PKEY_derive(ctx, shared_secret, &shared_secret_len); ``` 以上是使用gmssl库在C++中实现SM2算法的简要介绍。如果需要更详细的代码示例或者其他相关信息,可以参考gmssl官方文档或者在开发者社区进行咨询。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值