openssl 命令行 sm4 加解密

sms4 算法标准数据实例

密钥:0123456789abcdeffedcba9876543210

明文:0123456789abcdeffedcba9876543210

密文:681edf34d206965e86b3e94f536e4246

gmssl 命令:

mssl sms4-ecb -e -nopad -in plain.dat -K 0123456789abcdeffedcba9876543210 -out cipher.dat

gmssl sms4-ecb -d -nopad -in cipher.dat -K 0123456789abcdeffedcba9876543210 -out msg.dat

实例:

查看明文数据、对称加密、查看密文数据、对称解密、查看已解密数据

hexdump -C plain.dat

gmssl sms4-ecb -e -nopad -in plain.dat -K 0123456789abcdeffedcba9876543210 -out cipher.dat

hexdump -C cipher.dat 

gmssl sms4-ecb -d -nopad -in cipher.dat -K 0123456789abcdeffedcba9876543210 -out msg.dat

hexdump -C msg.dat

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
以下是使用 OpenSSL 库进 SM4 解密的 C 语言代码示例: ```c #include <openssl/evp.h> void sm4_encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *iv, unsigned char *ciphertext) { EVP_CIPHER_CTX *ctx; int len; int ciphertext_len; /* Create and initialize the context */ if (!(ctx = EVP_CIPHER_CTX_new())) { printf("Error creating SM4 context\n"); return; } /* Initialize the encryption operation */ if (1 != EVP_EncryptInit_ex(ctx, EVP_sm4_ecb(), NULL, key, iv)) { printf("Error initializing SM4 encryption\n"); EVP_CIPHER_CTX_free(ctx); return; } /* Encrypt the plaintext */ if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len)) { printf("Error encrypting SM4 plaintext\n"); EVP_CIPHER_CTX_free(ctx); return; } ciphertext_len = len; /* Finalize the encryption */ if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) { printf("Error finalizing SM4 encryption\n"); EVP_CIPHER_CTX_free(ctx); return; } ciphertext_len += len; /* Clean up */ EVP_CIPHER_CTX_free(ctx); } void sm4_decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, unsigned char *iv, unsigned char *plaintext) { EVP_CIPHER_CTX *ctx; int len; int plaintext_len; /* Create and initialize the context */ if (!(ctx = EVP_CIPHER_CTX_new())) { printf("Error creating SM4 context\n"); return; } /* Initialize the decryption operation */ if (1 != EVP_DecryptInit_ex(ctx, EVP_sm4_ecb(), NULL, key, iv)) { printf("Error initializing SM4 decryption\n"); EVP_CIPHER_CTX_free(ctx); return; } /* Decrypt the ciphertext */ if (1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len)) { printf("Error decrypting SM4 ciphertext\n"); EVP_CIPHER_CTX_free(ctx); return; } plaintext_len = len; /* Finalize the decryption */ if (1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) { printf("Error finalizing SM4 decryption\n"); EVP_CIPHER_CTX_free(ctx); return; } plaintext_len += len; /* Clean up */ EVP_CIPHER_CTX_free(ctx); } ``` 其中,`sm4_encrypt` 函数用于进 SM4 密,`sm4_decrypt` 函数用于进 SM4 解密。需要传入以下参数: - `plaintext`:需要密的明文数据。 - `plaintext_len`:明文数据的长度。 - `key`:密使用的密钥。 - `iv`:密使用的初始化向量。 - `ciphertext`:密后的密文数据(在 `sm4_encrypt` 函数内部生成)。 - `ciphertext_len`:密文数据的长度。 - `plaintext`:需要解密的密文数据。 - `plaintext_len`:密文数据的长度。 - `key`:解密使用的密钥。 - `iv`:解密使用的初始化向量。 - `plaintext`:解密后的明文数据(在 `sm4_decrypt` 函数内部生成)。 请注意,以上代码仅对 ECB 模式进SM4 解密,如果需要使用其他模式,请更改 `EVP_sm4_ecb()` 函数调用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值