Hello,
I'm new to cryptography and I'm working on my first project. I have
written a Java applet to encrypt files with 128bit AES in CBC mode using
PKCS5 padding. The 16 byte random IV is written to the file first, then
the encrypted data. The Java app can decrypt it's own output with no
problems. However, I need my C++ app to be able to decrypt it using
crypto++ and it currently doesn't work.
I get a cyrpto++ exception thrown when 'messageEnd()' is called:
StreamTransformationFilter: invalid PKCS #7 block padding found.
I think this is because the two apps are not using the same block
padding mode but I don't know how to change crypto++ to use the same
mode. It's possible that I've just made a newbie mistake - any help
would be fantastic!
Here's the relevant Java source code:
------------------------------------------------
SecretKeySpec skeySpec = new SecretKeySpec(secret_key, "AES");
// Instantiate the cipher
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(
input_text_area_.getText().getBytes() );
FileOutputStream output_stream = new
FileOutputStream(output_file);
output_stream.write( cipher.getIV() ); // write IV first
output_stream.write(encrypted);
output_stream.close();
---------------------------------------------------
Here's the C++:
---------------------------------------------------
std::ifstream licence_file_stream( licence_file_, std::ios::in |
std::ios::binary );
// Key and IV setup
const std::vector<byte> encryption_key =
Base64::Decode(encryption_password);
// read the first 16 byte block of data from the file - this is the
IV...
std::vector<byte> initialisation_vector(16);
licence_file_stream.read( (char*) &initialisation_vector[0],
initialisation_vector.size() );
// read the remaining binary file content - this is the cipher text...
std::vector<byte> cipher_text( file_size - 16 );
licence_file_stream.read( (char*) &cipher_text[0], cipher_text.size() );
licence_file_stream.close();
// Decrypt
CryptoPP::AES::Decryption aesDecryption( &encryption_key[0],
encryption_key.size() );
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption(
aesDecryption, &initialisation_vector[0] );
// init string sink...
std::string decrypted_text;
CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new
CryptoPP::StringSink( decrypted_text ) );
stfDecryptor.Put( &cipher_text[0], cipher_text.size() );
stfDecryptor.MessageEnd();
----------------------------------------------------
Thanks a lot for your help,
Ian