BadPaddingException in a decryption code

i am getting this errorjavax.crypto.BadPaddingException: Given final block not properly paddedi am trying to decrypt a cipher txt using a sec. key and store the message in a buffer.any solution?
[215 byte] By [sindya] at [2007-11-26 19:12:50]
# 1

> i am getting this error

> javax.crypto.BadPaddingException: Given final block

> not properly padded

>

> i am trying to decrypt a cipher txt using a sec. key

> and store the message in a buffer.any solution?

Given the vast amount of detail given above about the problem, I'd have to say:

Better make sure the final block gets properly padded.

warnerjaa at 2007-7-9 21:11:44 > top of Java-index,Java Essentials,Java Programming...
# 2
I ran into this same problem. Check your key lengths and make sure they match during enc/decryption
xplodersuva at 2007-7-9 21:11:45 > top of Java-index,Java Essentials,Java Programming...
# 3
there isnt any problem with key length...guess some prob with byte size
sindya at 2007-7-9 21:11:45 > top of Java-index,Java Essentials,Java Programming...
# 4
> there isnt any problem with key length...guess some> prob with byte sizeNo! I bet you are converting your encrypted data to a String using String encrptedDataAsString = new String(encrptedDataAsBytes);
sabre150a at 2007-7-9 21:11:45 > top of Java-index,Java Essentials,Java Programming...
# 5
got it!!!byte[ ] storing decrypted message should exclude padded bits
sindya at 2007-7-9 21:11:45 > top of Java-index,Java Essentials,Java Programming...
# 6

> got it!!!

> byte[ ] storing decrypted message should exclude

> padded bits

Since the BadPaddingException is thrown when you are decrypting, I don't see how this can apply. If you are using PKCS5Padding (as is implied by the the exception) then when you decrypt the padding bytes will automatically be removed. No need for you to take any action.

Why not post some code so that we can understand what you are doing. Make sure you use the [code] tags.

sabre150a at 2007-7-9 21:11:45 > top of Java-index,Java Essentials,Java Programming...
# 7
Could you please put ur code i can figure it out what 's the exact problem.
Manjaya at 2007-7-9 21:11:45 > top of Java-index,Java Essentials,Java Programming...
# 8
sabre said what I was going to suggest - it's most common to see that if you've converted your byte arrays to Strings at any point in the code.
DavidKNa at 2007-7-9 21:11:45 > top of Java-index,Java Essentials,Java Programming...
# 9

byte[] decrypt (byte[] cipher, SecretKeySpec key) throws Exception

{

Cipher aesCipher;

aesCipher = Cipher.getInstance("AES");

System.out.println("check");

aesCipher.init(Cipher.DECRYPT_MODE, key);

// byte[] original=new byte[1024];

byte[] original = aesCipher.doFinal(cipher);

System.out.println("text"+ original);

return original;

}

sindya at 2007-7-9 21:11:45 > top of Java-index,Java Essentials,Java Programming...
# 10

Since the BadPaddingException is thrown when you are decrypting, I don't see how this can apply. If you are using PKCS5Padding (as is implied by the the exception) then when you decrypt the padding bytes will automatically be removed. No need for you to take any action.

i have used PKCS5Padding....so the original ( above code) is the plain text with padding removed right?

and i get a new exception "IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher"

Message was edited by:

sindy

sindya at 2007-7-9 21:11:45 > top of Java-index,Java Essentials,Java Programming...