c - 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。
然而,我得到两个不同的输出:
- 执行副总裁+C:
HBy5KT15kp+dLFuBNU15rw==
- CLI:(
zcTjiVTkZI8XSpDbc0HvRA==
我对具有相同参数的 Java 程序得到相同的结果)
知道为什么这些会生成不同的输出,因此无法在 CLI 中解密 EVP 输出。
解决方案
推荐阅读
- javascript - 有没有办法从单击更改为悬停在我的缩略图库上?
- python - 如何解析 XML 文件中的 VCARD
- user-interface - 可访问的对比度与企业标识指南
- c# - 订阅正在执行的任何方法
- angular - 我无法理解 ngFor 循环中索引值的作用以及它如何帮助创建动态表单字段
- python - 创建评分从 django 开始
- python - 导入错误 - ModuleNotFoundError: No module named 'model'
- selenium-webdriver - 使用 selenium webdriver 的多个屏幕截图
- javascript - 是否可以获得元素的 MutationObserver 实例?
- c# - 子类唯一静态变量:父声明、子初始化、自动克隆静态方法