C语言 无法使用 openssl CLI 解密消息,该 CLI 是使用 openssl API 加密的



我使用的是Linux libcrypto AES-128中的上述代码 CBC Encryption/Decryption 适用于 Ubuntu,但不适用于 Raspberry Pi

在这里稍微修改了代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/aes.h>
#include <openssl/crypto.h>  
unsigned char aes_key[]={0x0,0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9,0xa,0xb,0xc,0xd,0xe,0xf};
/* Print Encrypted and Decrypted data packets */
void print_data(const char *tittle, const void* data, int len);
int main( )
{
/* Input data to encrypt */
unsigned char aes_input[]={0x0,0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9,0xa,0xb,0xc,0xd,0xe,0xf};
fprintf(stderr,"%sn",SSLeay_version(SSLEAY_VERSION));
/* Init vector */
//unsigned char iv[AES_BLOCK_SIZE];
unsigned char iv[]={0x0,0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9,0xa,0xb,0xc,0xd,0xe,0xf};
//memset(iv, 0x00, AES_BLOCK_SIZE);
/* Buffers for Encryption and Decryption */
unsigned char enc_out[sizeof(aes_input)];
unsigned char dec_out[sizeof(aes_input)];
/* AES-128 bit CBC Encryption */
AES_KEY enc_key, dec_key;
AES_set_encrypt_key(aes_key, sizeof(aes_key)*8, &enc_key);
AES_cbc_encrypt(aes_input, enc_out, sizeof(aes_input), &enc_key, iv,         AES_ENCRYPT);
FILE *fp = fopen("id_encrypted","w");
fwrite(enc_out, sizeof(enc_out), 1, fp); // write all the new buffer
fclose(fp);
/* AES-128 bit CBC Decryption */
//memset(iv, 0x00, AES_BLOCK_SIZE); // don't forget to set iv vector again, else you can't decrypt data properly
//unsigned char iv[AES_BLOCK_SIZE];
unsigned char iv2[]={0x0,0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9,0xa,0xb,0xc,0xd,0xe,0xf};
AES_set_decrypt_key(aes_key, sizeof(aes_key)*8, &dec_key); // Size of key is in bits
AES_cbc_encrypt(enc_out, dec_out, sizeof(aes_input), &dec_key, iv2, AES_DECRYPT);
/* Printing and Verifying */    
print_data("n Original ",aes_input, sizeof(aes_input)); // you can not print data as a string, because after Encryption its not ASCII
print_data("n Encrypted",enc_out, sizeof(enc_out));
print_data("n Decrypted",dec_out, sizeof(dec_out));
return 0;
}
void print_data(const char *tittle, const void* data, int len)
{
printf("%s : ",tittle);
const unsigned char * p = (const unsigned char*)data;
int i = 0;
for (; i<len; ++i)
printf("%02X ", *p++);
printf("n");
}

如果运行代码,则可以看到以下输出。

OpenSSL 1.1.1 11 九月 2018

原文 : 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

加密 : C6 A1 3B 37 87 8F 5B 82 6F 4F 81 62 A1 C8 D8 79

解密 : 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

我的目标是使用 openssl CLI 解密加密的输出文件 [在代码中命名为id_encrypted]。

我使用的命令是

openssl enc -d -v -aes-128-cbc -in id_encrypted -out id_decrypted -K "0123456789abcdef" -iv "0123456789abcdef"

但是得到下面的错误消息,输出文件是空的。

bufsize=8192
hex string is too short, padding with zero bytes to length
hex string is too short, padding with zero bytes to length
bad decrypt
139861660053952:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:../crypto/evp/evp_enc.c:537:

任何人都可以帮助为什么它不起作用?我想,问题出在我正在使用的openssl命令上,因为我们可以看到该代码适用于加密和解密。

不要以这种方式编写加密代码。您正在使用低级 AES API,该 API 适用于非常了解他们正在做什么的高级用户。特别是它们不会:

  • 执行填充 - 这在大多数情况下是必需的(并且在OpenSSL CLI解密期间是预期的(
  • 默认使用优化和恒定时间(即最安全(的实现

它们也很容易被滥用并搬起石头砸自己的脚。由于这个原因和上述原因,OpenSSL项目团队长期以来一直不鼓励使用低级AES API,并将在下一版本的OpenSSL中弃用:

https://github.com/openssl/openssl/blob/9ce921f2dacc9f56b8ae932ae9c299670700a297/CHANGES#L394-L413

相反,您应该使用 EVP API。有关示例代码,请参阅此页面:

https://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption

除此之外,在OpenSSL CLI上指定密钥和iv的方式上,您还有一些其他问题。"-K"参数需要十六进制的键。每个十六进制数字代表 4 位(不是 8 位(。所以你给出的密钥等效于:

01 23 45 67 89 ab cd ef

但你想要的是:

00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f

IV也有类似的问题。所以你真正的意图是:

$ openssl enc -d -v -aes-128-cbc -in id_encrypted -out id_decrypted -K "000102030405060708090a0b0c0d0e0f" -iv "000102030405060708090a0b0c0d0e0f"

但是,在您修复加密代码以进行正确的填充之前,这仍然不起作用。

编辑添加:

您还可以使用-nopad选项在不填充的情况下进行解密。但是,这仍然最好重写代码以不需要这样做。

最新更新