Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何用c语言翻译openssl命令pbkdf2?_C++_C_Openssl_Sha256_Pbkdf2 - Fatal编程技术网

C++ 如何用c语言翻译openssl命令pbkdf2?

C++ 如何用c语言翻译openssl命令pbkdf2?,c++,c,openssl,sha256,pbkdf2,C++,C,Openssl,Sha256,Pbkdf2,伙计们,关于openssl的另一个问题是: 我有这个命令: openssl enc -aes-192-cbc -pbkdf2 -e -in <infile> -out <outfile> -pass pass:password openssl enc-aes-192-cbc-pbkdf2-e-in-out-pass密码 现在我只有这个命令的密文,我知道密码,然后我还知道salt,它是字符串“Salted_uuu_u”后密文的前8个字节。我还知道openssl作为默认

伙计们,关于openssl的另一个问题是:

我有这个命令:

openssl enc -aes-192-cbc -pbkdf2 -e -in <infile> -out <outfile> -pass pass:password 
openssl enc-aes-192-cbc-pbkdf2-e-in-out-pass密码
现在我只有这个命令的密文,我知道密码,然后我还知道salt,它是字符串“Salted_uuu_u”后密文的前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("\n\n");

    //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);

}
void decrypt(无符号字符*密文、整数密文、无符号字符*明文、常量字符*密码、无符号字符*salt){
EVP_CIPHER_CTX*CTX;
内伦;
int纯文本;
//无符号字符*key=“EF04020D6979FC09CC1859A2BFB832FFCF57C64BA61F682”右值
//无符号字符*iv=“722EDDC813763C9AAB96A0A6885CE1DB”右值
无符号字符键[24],iv[16];
int i;
PKCS5_PBKDF2_HMAC(密码,strlen(密码),(未签名字符*)salt,strlen(salt),1000,EVP_sha256(),16,密钥);
EVP_BytesToKey(EVP_aes_192_cbc(),EVP_sha256(),(无符号字符*)salt,(无符号字符*)密码,strlen(密码),1000,密钥+16,iv);
printf(“键:”;对于(i=0;i<24;++i){printf(“%02x”,键[i]);}printf(“\n”);
printf(“IV:”;对于(i=0;i<16;++i){printf(“%02x”,IV[i]);}printf(“\n\n”);
//创建并初始化上下文
如果(!(ctx=EVP\u CIPHER\u ctx\u new())
handleErrors();
//初始化解密操作。重要信息-确保使用密钥
//和适合您密码的IV大小
if(1!=EVP_DecryptInit_ex(ctx,EVP_aes_192_cbc(),NULL,key,iv))
handleErrors();
//提供要解密的消息,并获得明文输出。
//如有必要,可以多次调用EVP_DecryptUpdate。
if(1!=EVP_DecryptUpdate(ctx、明文和len、密文、密文_len))
handleErrors();
明文_len=len;
//完成解密。可在以下位置写入更多明文字节:
//这个阶段。
如果(1!=执行副总裁(ctx、明文+len和len))
handleErrors();
明文_len+=len;
//清理
无密码(CTX)的执行副总裁;
}
正确的值被注释,我从终端获取它。。。。但是我的c代码不返回该值。
我认为这取决于我没有很好地翻译-pbkdf2派生密钥的事实……请帮帮我

我在GitHub上的openssl源代码中解决了这个问题:


在调试器中运行程序如何?