【发布时间】: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