Click here to Skip to main content
15,878,959 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use crypto++ to encrypt text with AES and save to File,then read file decrypt the text,but the text decrypt not exactly the same with the original text? I don't know why?
exapme:
original text:/*+++++++lskjdflksdfj
decrypt text: /*++1++++lskjdflksdfj


only one byte not the same!
The code is Here, I'am sure the key and iv is the same;

Encrypt code:

C++
    std::string strCiphertext = "";
AES::Encryption aesEncryption(mKey, mKeyLen);

string cipher;
CBC_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, mIv);
StreamTransformationFilter stfEncryptor(cbcEncryption, new HexEncoder(new StringSink(cipher)));
stfEncryptor.Put((byte*)(plainText.c_str()), plainText.length());
stfEncryptor.MessageEnd();

Decrypt code:

C++
  string encoded;
StringSource s(cryptText, true, new HexDecoder(new StringSink(encoded)));
//设置aes 密钥
//byte key[AES::DEFAULT_KEYLENGTH] = "1234567890";
//设置初始向量组, 可以默认随机,但是一旦指定,加密和解密必须使用相同的初始向量组
//byte iv[AES::BLOCKSIZE] = "sky";
 //解密
Log::log("before ...");
std::string strDecryptedText = "";
try
{
    AESDecryption aesDecryption(mKey, mKeyLen);
    CBC_Mode_ExternalCipher::Decryption cbcDecryption(aesDecryption, mIv);
    StreamTransformationFilter stfDecryptor(cbcDecryption, new StringSink(strDecryptedText));
    stfDecryptor.Put(reinterpret_cast<const unsigned="" char="">(encoded.c_str()), encoded.length());
    stfDecryptor.MessageEnd();

    Log::log(strDecryptedText);
}
catch (const std::exception& ex)
{
    Log::log(ex.what());
}
Log::log("after ...");


Any one can help me,thanks a lot!
Posted
Updated 24-Feb-14 3:45am
v2
Comments
[no name] 24-Feb-14 18:43pm    
This looks weird:

stfDecryptor.Put(reinterpret_cast<const unsigned="" char="">(encoded.c_str()), encoded.length());
pasztorpisti 24-Feb-14 20:13pm    
The code seems to be OK for me at first sight. If you save the the data into a file and then you load it back (or if you serialize/deserialize it to/from somewhere else) then make sure that the data doesn't change during serialization, for example if you use fopen then make sure that you open the file in binary mode (not "w" and "r", instead "wb" and "rb") also make sure that the data is the same after saving and loading using a debugger. If you serialize then also make sure that there is no extra newline character at the end of ciphertext, check the length of the data too!
CmMac 25-Feb-14 6:06am    
I am sure the string encoded before saving is the same to the string load from file.The code run OK in debug version, but only has problem is release version.Why, is the crypto lib bug?
Rage 25-Feb-14 7:27am    
Is the problem always happening on the same byte (no matter how long the string ?)
CmMac 25-Feb-14 8:07am    
almost the same byte,other place is ok.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900