如何用c语言翻译openssl命令pbkdf2
How to translate openssl command pbkdf2 in c languge?
Hy guys,关于openssl的另一个问题:
我有这个命令:
openssl enc -aes-192-cbc -pbkdf2 -e -in <infile> -out <outfile> -pass pass:password
现在我只有这个命令的密文,我知道密码,然后我也知道salt,它是字符串"Salted__"之后密文的前8个字节。我还知道openssl作为默认参数使用1000次迭代计数,sha256作为摘要。我的问题是:如何回到c语言的派生键和派生iv?
我的代码是这样的,但它没有返回正确的值:
void decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *plaintext, const char *password, unsigned char* salt){
EVP_CIPHER_CTX *ctx;
int len;
int plaintext_len;
//unsigned char* key = "EF04020D6979FC09CC1859A2BFB832FFFCF57C64BA61F682"; right value
//unsigned char* iv = "722EDDC813763C9AAB96A0A6885CE1DB"; right value
unsigned char key[24], iv[16];
int i;
PKCS5_PBKDF2_HMAC(password, strlen(password), (unsigned char*)salt, strlen(salt), 1000, EVP_sha256(), 16, key);
EVP_BytesToKey(EVP_aes_192_cbc(), EVP_sha256(), (unsigned char*)salt, (unsigned char*)password, strlen(password), 1000, key+16, iv);
printf("Key: "); for(i=0; i < 24; ++i){ printf("%02x", key[i]); } printf("n");
printf("IV: "); for(i=0; i < 16; ++i){ printf("%02x", iv[i]); } printf("nn");
//Create and initialise the context
if(!(ctx = EVP_CIPHER_CTX_new()))
handleErrors();
//Initialise the decryption operation. IMPORTANT - ensure you use a key
//and IV size appropriate for your cipher
if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_192_cbc(), NULL, key, iv))
handleErrors();
//Provide the message to be decrypted, and obtain the plaintext output.
//EVP_DecryptUpdate can be called multiple times if necessary.
if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
handleErrors();
plaintext_len = len;
//Finalise the decryption. Further plaintext bytes may be written at
//this stage.
if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len))
handleErrors();
plaintext_len += len;
//Clean up
EVP_CIPHER_CTX_free(ctx);
}
对正确的值进行了注释,我从终端中获取。。。。但是我的c代码不返回这些值。我认为这取决于我没有很好地翻译-pbkdf2派生密钥。。。请帮帮我
我在GitHub上的openssl源代码中看到了这个问题:
https://github.com/openssl/openssl/blob/master/apps/enc.c
相关文章:
- 从命令行c++发送文本文件名
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 如何在openssl-ecc中获取十六进制格式的私钥
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- LINK 尝试使用 OpenSSL evp aes 256 c++ 时出错
- 使用QProcess执行命令,并将结果存储在QStringList中
- 数组长度,为什么从命令行获取时不能使用它?
- 如果用户输入无效,如何使用字符串变量-C++重复输入命令
- clang整洁10忽略了我的NOLINT命令
- 在子目录中使用target_sources()命令时用于单元测试(qtest)的项目结构
- 通过 Openssl 命令行加密,通过 c++ 解密
- 将命令行转换为从 Openssl 到 c++ 的代码
- 如何用c语言翻译openssl命令pbkdf2
- openssl rc4 命令行加密和 cpp 文件实现 rc4 之间的区别
- OpenSSL-通过代码生成的数字签名与在命令行生成的签名不匹配
- OpenSSL摘要:命令行和c++中的不同结果
- OpenSSL C++RSA符号与命令行符号不同
- 在 HPIA 上使用'chatr'命令更改 OpenSSL 库内部名称