Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

problems with BadPaddingException

843811Jul 27 2006 — edited Jul 28 2006
Hi friends...

I have a problem with class javax.crypto.Cipher.
I have created the following functions for encrypting and decrypting data:

public static void sendEncryptedBytes(byte[] bytesToSend, Key key,
DataOutputStream dos) throws Exception {
Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte [ ] iv = cipher.getIV();
dos.writeInt(iv.length);
dos.write(iv);
byte [ ] encryptedBytes = cipher.doFinal(bytesToSend);
dos.writeInt(encryptedBytes.length);
dos.write(encryptedBytes);
}


public static byte[] receiveEncryptedBytes(Key key, DataInputStream dis) throws
Exception {

Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
byte [ ] iv = new byte[dis.readInt()];
dis.readFully(iv);
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
cipher.init(Cipher.DECRYPT_MODE, key);
byte [ ]encryptedBytes = new byte[dis.readInt()];
dis.readFully(encryptedBytes);
return cipher.doFinal(encryptedBytes);
}

If I encrypt and decrypt with the same key I haven't any problems.
If I encrypt with a key and I decrypt with a different key the program trows the following exception:

javax.crypto.BadPaddingException: Given final block not properly padded

I'd like that the data encryption operation doesn't fail beacause I'd like to use this data for the following control.
I'm writing an implementation of an authentication protocol, EKE (Encrypted Key Exchange).

I'm totally desperate!!


Thank's,
Francy
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 25 2006
Added on Jul 27 2006
4 comments
545 views