Re: Encrypt with JCE (AES/CBC/PKCS5Padding), decrypt with crypto++ v521 - doesn't work!
Wei Dai <weidai <at> weidai.com>
2006-10-30 18:40:53 GMT
PKCS #5 padding and PKCS #7 padding are essentially the same thing. See
http://www.users.zetnet.co.uk/hopwood/crypto/scan/cs.html#pad_PKCSPadding.
There is probably something else going wrong. For example not using the same
key or IV on both sides would also produce this error, since the decrypted
padding would be random. I suggest using the debugger and step through the
code to see what is going on.
----- Original Message -----
From: "Ian Thomas" <thomas <at> ccdc.cam.ac.uk>
To: <cryptopp-list <at> eskimo.com>
Sent: Monday, October 30, 2006 11:47 PM
Subject: Re: Encrypt with JCE (AES/CBC/PKCS5Padding), decrypt with crypto++
v521 - doesn't work!
> Hi Haytham,
>
> Thanks for your advice. I just made your recommended change but I now get
> an exception from my Java app:
>
> "Cannot find any provider supporting AES/CBC/PKCS7Padding"
>
> Does this mean that I won't be able to encrypt in Java and decrypt using
> Crypto++ because they can't use a mutually compatible padding system, or
> is there another one I can use that is supported by both systems? Is it
> not possible to configure Crypto++ to use "PKCS5Padding"?
>
> Thanks again,
>
> Ian
>
>
> Haytham Mohammed wrote:
>> Hi Thomas
>> Instantiate the cipher with
>> Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
>> instead of Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
>> Regards
>> Haytham Mohammed
>> ----- Original Message ----
>> From: Ian Thomas <thomas <at> ccdc.cam.ac.uk>
>> To: cryptopp-list <at> eskimo.com
>> Sent: Monday, October 30, 2006 2:52:48 PM
>> Subject: Encrypt with JCE (AES/CBC/PKCS5Padding), decrypt with crypto++
>> v521 - doesn't work!
>>
>> 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
>>
>>
>