【问题标题】:openssl decrypt function doesn't work correctlyopenssl 解密功能无法正常工作
【发布时间】:2020-07-03 08:57:00
【问题描述】:

我通过查看 wikiopenssl 编写了这个函数,当我尝试在 main func 中使用它时它工作正常。然后我写了另一个函数来只用脚本来解密。这是我的功能:

std::string ofxLibcrypto::decrypt(std::string ciphertext) {

unsigned char *plaintext;
unsigned char *binarytobedecrypted;

hex2stream(ciphertext, ciphertext);

for(int i = 0; i < ciphertext.length(); i++){
    binarytobedecrypted[i] = ciphertext[i];
}

int lenplaintext = decrypt(binarytobedecrypted, strlen((char*)binarytobedecrypted), k, v, plaintext);

std::string str(reinterpret_cast<char*>(plaintext));

clean();

ofLogVerbose("ofxLibcrypto.cpp") << "decrypted";

return str.substr(0, lenplaintext);
}

hex2stream 是一个将 base16 字符串解码为二进制的函数,并且工作正常。第一个参数是要解码的字符串,第二个参数是保存二进制值。我看到问题出在解密函数中,这里是解密函数:

int ofxLibcrypto::decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, unsigned char *iv, unsigned char *plaintext) {

EVP_CIPHER_CTX *ctx;
int len;
int plaintext_len;

if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
ofLogNotice("IDB") << "To detect 1";

if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv)) handleErrors();
ofLogNotice("IDB") << "To detect 2";

if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len)) handleErrors();
ofLogNotice("IDB") << "To detect 3";

plaintext_len = len;
ofLogNotice("IDB") << "To detect 4";

if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) handleErrors();
ofLogNotice("IDB") << "To detect 5";

plaintext_len += len;
ofLogNotice("IDB") << "To detect 6";

EVP_CIPHER_CTX_free(ctx);
ofLogNotice("IDB") << "To detect 7";

return plaintext_len;
}

LogNotices 用于检测问题出在哪里,它会打印出“检测 4”,然后应用程序崩溃。我尝试了很多来解决,但我找不到解决方案。已经谢谢大家了。

【问题讨论】:

  • 加密数据不是字符串,不能在上面使用strlen
  • @user253751 我把那行写成了` int lenplaintext = decrypt((unsigned char*)ciphertext.c_str(), ciphertext.length(), k, v, plaintext); ` 但还是不行

标签: c++ encryption openssl aes


【解决方案1】:

plaintext 未初始化。因此,它指向垃圾内存。当EVP_DecryptUpdate 写入时,也就是未定义的行为。

另外,您也忘记分配binarytobedecrypted

这样做。已经很晚了,但我猜你应该给明文一些额外的填充,无论你的加密算法使用什么块大小。

binarytobedecrypted = new unsigned char[ciphertext.size()+1];
plaintext = new unsigned char[(ciphertext.size() + 128];

如果您认为该数据是“字符串”,请确保在写入缓冲区后为 null 终止。

if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len))
{
    handleErrors();
}
else
{
    plaintext[len] = '\0';
}
ofLogNotice("IDB") << "To detect 5";

【讨论】:

    【解决方案2】:

    我将解密函数更改为:

    std::string ofxLibcrypto::decrypt(std::string ciphertext) {
    unsigned char empty[] = { 0 };
    unsigned char *plaintext = empty;
    hex2stream(ciphertext, ciphertext);
    int lenplaintext = decrypt((unsigned char *)ciphertext.c_str(), 
    (int)ciphertext.length(), k, v, plaintext);
    std::string str(reinterpret_cast<char*>(plaintext));
    clean();
    ofLogVerbose("ofxLibcrypto.cpp") << "decrypted";
    
    return str.substr(0, lenplaintext);
    }
    

    在此之后,同样的问题再次发生,但我发现问题是纯文本的 - 加密的文本 - 长度。它适用于 > 24 个字符。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-27
      • 2017-09-23
      • 2019-01-26
      • 1970-01-01
      • 2013-11-06
      • 2013-10-29
      • 1970-01-01
      相关资源
      最近更新 更多