首页 > 解决方案 > AES256 的 OpenSSL EVP API 和 CLI 不匹配

问题描述

我正在尝试制作一个 C 程序和 openssl CLI 为 AES 加密生成一致的输出。

对于加密,我使用这个 C 和 OpenSSL EVP 示例:https ://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption#Encrypting_the_message

它的调用方式如下:

....
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
....

int main()
{
    char ciphertext[1024];
    static char *enckey = (char *) "7c07f68ea8494b2f8b9fea297119350d78708afa69c1c7600000000000000000";
    static char *iv = (char *)"FEDCBA09876543210000000000000000";
    size_t* output_length;
    
    ssize_t crypto_length = encrypt("test", 4, enckey, iv, ciphertext);
    
    printf("%s", base64_encode(ciphertext, crypto_length, output_length) );
}

对于 OpenSSL CLI,我正在使用:

openssl enc -aes-256-cbc -e -a -A -in input.dat \
  -K 7c07f68ea8494b2f8b9fea297119350d78708afa69c1c7600000000000000000 -iv FEDCBA09876543210000000000000000

相同的密钥,IV,明文(input.dat 里面只有“测试”(不带引号)),相同的模式 - AES-256-CBC。

然而,我得到两个不同的输出:

知道为什么这些会生成不同的输出,因此无法在 CLI 中解密 EVP 输出。

标签: cencryptionopenssl

解决方案


推荐阅读