OpenSSL命令行进行sm4加解密的问题

        在使用openssl验证自己的程序实现的sm4算法是否正确的时候,遇到加解密与自己程序加解密结果不一致的情况,分析是对openssl的用法有错误。

        先展示一下最终正确使用的情况:

        KEY:31323334353637383930313233343536(字符串"1234567890123456"的utf8编码)

        IV:31323334353637383930313233343536(字符串"1234567890123456"的utf8编码)

        明文:31323334353637383930313233343536(字符串"1234567890123456"的utf8编码)

        算法:SM4-CBC

        填充:PKCS7

        使用openssl加密:

[postgres@localhost bin]$ ./openssl sm4-cbc -e  -K 31323334353637383930313233343536 -iv 31323334353637383930313233343536 -in test.in -out test.out     
[postgres@localhost bin]$ hexdump -C ./test.out
00000000  d2 d6 8e d9 fe 06 cb 40  c9 a1 50 aa 59 17 f1 5f  |.......@..P.Y.._|
00000010  e2 65 62 9d 4e 46 1d 14  7c 1d 88 b8 00 c6 ab ba  |.eb.NF..|.......|
00000020
[postgres@localhost bin]$

        再看在线sm4加密结果:

        openssl和在线加密工具加密结果是一致的。

        把遇到的问题做一次总结:

        1、openssl是以二进制形式读取输入文件test.in(加密内容)的,输入字符串”1234567890123456“到文件test.in,openssl实际读到的是313233343536373839303132333435360a,最后多了一个0a,需要手动去除;

        2、通过参数-K和-iv传入的密钥和iv,openssl也是识别为二进制,如果将字符串"1234567890123456"作为密钥,需要如上输入 -K 31323334353637383930313233343536,同理-iv也是这样;

        3、openssl默认使用PKCS7进行填充,如果不需要填充可以使用参数-nopad;

        4、如果给予的密钥(-K)和IV(-iv)不足16字节,openssl默认使用二进制0进行填充,即如果给予的是31323334353637383930,实际使用的是31323334353637383930313233343536000000000000。

         

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 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()` 函数调用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值