Javax.crypto.BadPaddingException?

Ok, so the problem is that you are converting the encrypted bytes to a hex string (using the asHex method) but are not converting the hex string back to a byte array correctly for decryption. You can't use getBytes.

Ok, so the problem is that you are converting the encrypted bytes to a hex string (using the asHex method) but are not converting the hex string back to a byte array correctly for decryption. You can't use getBytes. You can use the following method to convert a hex string to a byte array: public static byte fromHexString(String s) { int len = s.length(); byte data = new bytelen / 2; for (int I = 0; I Digit(s.

CharAt(i), 16) CharAt(i+1), 16)); } return data; } and then change your decrypt method to use: original = cipher. DoFinal(fromHexString(message)).

Dogbane, I make as you mention and padd the reuturn value manully ,and the size of cipher msg is 128 but still have the same exception. – Alaa Jan 3 at 0:07 @Alaa I have updated my answer. I didn't know you were converting to hex.

– dogbane Jan 3 at 0:31 @dogbane,I did but the same error. I wonder if it has any related to read or write to files, since I store the cipher in a file then read it to decrypt , I am using BufferedReader to read the file and return the content in StringBilder. – Alaa Jan 3 at 0:42 @dagbane, I add the method which read form the file .

– Alaa Jan 3 at 0:46 @Alaa are you using the SAME secret key for both encryption and decryption? Are you sure you are not generating a different key? – dogbane Jan 3 at 0:51.

I guess the expression message.trim().getBytes() does not return the same bytes which are generated when you encrypted the message. Specially the trim() method could delete the bytes which were added as padding in the encrypted message. Verify that both the returned array of the doFinal() method during the encryption and the returned array of message.trim().getBytes(): got the same number of bytes (array length) got the same bytes in the array.

The length if message.getBytes() is 128 but the exception happen in doFinal() method so I can't know the size of the return value . – Alaa Jan 2 at 23:23 @Alaa: I mean the returned byte array at the encryption, where you generated the encrypted message. That the length of message.getBytes() is 128 looks good, but you are using message.trim().getBytes().

So check the returned array of the encryption doFinal() call and the returned array of the expression message.trim().getBytes(), they must be the same. – Progman Jan 2 at 23:26 why doesn't getBytes() have a charset parameter? – free_easy Jan 2 at 23:32 both are the same size 128 and that sensible, since I pass the cipher value to be decrypted.

– Alaa Jan 2 at 23:37 @free_easy, how can charset help me? – Alaa Jan 2 at 23:39.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions