简体   繁体   English

如何使用C API解码openssl命令行生成的aes-256-cbc文件?

[英]How to use C API to decode the aes-256-cbc file generated by openssl command line?

I have read a link , and I can encode/decode data by C API correctly. 我已阅读的链接 ,我可以由C API正确地进行编码/解码的数据。

However, I have a file that is generated by openssl command line: 不过,我有一个由OpenSSL的命令行生成的文件:

openssl aes-256-cbc -in plain.txt -out encrypted.txt
> enter password from stdin

Want to decode the output by using C API. 要使用C API来解码输出。 The code is below: 代码如下:

unsigned char key[] = "password";
unsigned char iv[] = "";  // I don't know what is the default iv.
EVP_CipherInit(&ctx, EVP_aes_256_cbc(), key, iv, 0);

The output is wrong. 输出错误。 Is there anyone know how to fix it? 有谁知道如何解决? Maybe I called the wrong function? 也许我叫错了功能? eg: EVP_PBE_CipherInit? 例如:EVP_PBE_CipherInit? I'm totally lost in the document and source code of openssl. 我完全失去了OpenSSL中的文档和源代码。

Any advice is welcome. 欢迎任何建议。 Thank you in advance. 先感谢您。

The openssl manpage says the key and iv values are generated from the password if not supplied. OpenSSL的手册页说从密码中生成的密钥和IV值,如果没有提供。 Also it gives a -P option to print the salt, key and iv value. 此外,它还提供了-P选项来打印salt,key和iv值。 (This is the key that needs to be passed to the function, not the password). (这是一个需要被传递给函数,而不是密码的密钥)。

The salt value can be obtained from the file header. 盐值可以从文件头获得。 Then the key and iv has to regenerated using the same. 然后,密钥和iv必须使用相同的密钥重新生成。

This stackexchange page gives a very detailed answer. 这个stackexchange 页面提供了非常详细的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM