PDA

View Full Version : Cryptopp Help



ttimt
15th March 2013, 10:00
My program could encrypt text and save it in a file, and decrypt the cipher text after getting from the file.

But I keep on getting this error


terminate called after throwing an instance of 'CryptoPP::InvalidCiphertext'
what(): StreamTransformationFilter: ciphertext length is not a multiple of block size
exited with code 3

Here my encrypt code, the reason I seperate them to different files is because I'm trying to remove this annoying error.


byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ];
memset( key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH );
memset( iv, 0x00, CryptoPP::AES::BLOCKSIZE );
string ciphertext[7];



CryptoPP::AES::Encryption aesEncryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, iv );

CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink( ciphertext[0] ) );
stfEncryptor.Put( reinterpret_cast<const unsigned char*>( line[0].c_str() ), line[0].length() + 1 );
stfEncryptor.MessageEnd();

ofstream outfile("./QA.dll");
outfile << ciphertext[0] << endl;



CryptoPP::AES::Encryption aesEncryption1(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption1( aesEncryption1, iv );

CryptoPP::StreamTransformationFilter stfEncryptor1(cbcEncryption1, new CryptoPP::StringSink( ciphertext[1] ) );
stfEncryptor1.Put( reinterpret_cast<const unsigned char*>( line[1].c_str() ), line[1].length() + 1 );
stfEncryptor1.MessageEnd();

ofstream outfile1("./QF.dll");
outfile1 << ciphertext[1] << endl;



CryptoPP::AES::Encryption aesEncryption2(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption2( aesEncryption2, iv );

CryptoPP::StreamTransformationFilter stfEncryptor2(cbcEncryption2, new CryptoPP::StringSink( ciphertext[2] ) );
stfEncryptor2.Put( reinterpret_cast<const unsigned char*>( line[2].c_str() ), line[2].length() + 1 );
stfEncryptor2.MessageEnd();

ofstream outfile2("./QG.dll");
outfile2 << ciphertext[2] << endl;
....



Here decrypt's code, I made key1, key2 is also because of trying to remove the error :)


ifstream infile("./Q.dll");
if (!infile.is_open())
return 1;
getline(infile, ciphertext[0]);

ifstream infile1("./QF.dll");
if (!infile1.is_open())
return 1;
getline(infile1, ciphertext[1]);

ifstream infile2("./QG.dll");
if (!infile2.is_open())
return 1;
getline(infile2, ciphertext[2]);
...

// Decrypt other passwords
// **********************
// **********************
// **********************
// **********************
// **********************
// Set key


byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ];
memset( key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH );
memset( iv, 0x00, CryptoPP::AES::BLOCKSIZE );



// DECRYPT START~~~

if (ciphertext[0].size() != 0)
{
CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, iv );

CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( qdec[0] ) );
stfDecryptor.Put( reinterpret_cast<const unsigned char*>( ciphertext[0].c_str() ), ciphertext[0].size() );
stfDecryptor.MessageEnd();
QApplication::processEvents();
}
// **********************


byte key1[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv1[ CryptoPP::AES::BLOCKSIZE ];
memset( key1, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH );
memset( iv1, 0x00, CryptoPP::AES::BLOCKSIZE );


if (ciphertext[1].size() != 0)
{
CryptoPP::AES::Decryption aesDecryption1(key1, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption1( aesDecryption1, iv1 );

CryptoPP::StreamTransformationFilter stfDecryptor1(cbcDecryption1, new CryptoPP::StringSink( qdec[1] ) );
stfDecryptor1.Put( reinterpret_cast<const unsigned char*>( ciphertext[1].c_str() ), ciphertext[1].size() );
stfDecryptor1.MessageEnd();
QApplication::processEvents();
}

// **********************


byte key2[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv2[ CryptoPP::AES::BLOCKSIZE ];
memset( key2, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH );
memset( iv2, 0x00, CryptoPP::AES::BLOCKSIZE );


if (ciphertext[2].size() != 0)
{
CryptoPP::AES::Decryption aesDecryption2(key2, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption2( aesDecryption2, iv2 );

CryptoPP::StreamTransformationFilter stfDecryptor2(cbcDecryption2, new CryptoPP::StringSink( qdec[2] ) );
stfDecryptor2.Put( reinterpret_cast<const unsigned char*>( ciphertext[2].c_str() ), ciphertext[2].size() );
stfDecryptor2.MessageEnd();
QApplication::processEvents();
}
......
// **********************

And also, this is AES, but where do I actually set the secret key?

ttimt
17th March 2013, 04:50
I found out that this error occurs is related to the plain text inserted. And now I guess is because of some unicode while reading. I ran gdb and check the cipher size and found out that 1 of them is 14 byte instead of 16 and when I print it out it shows only 3/4 of the cipher. The input file is 16 byte. So it seems I need to try to read unicode from file. But how to read?