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!

BadPaddingException: Given final block not properly padded

843810Jun 22 2003 — edited Jun 23 2003
Hi,

I am trying to encrypt and decrypt data in the code below. It works fine as long as I save the encrypted string in a file using ObjectOutputStream and then read the same file to decrypt it. My problem is: I have to store the encrypted data in a DB2 table (on mainframe). When I try to decrypt the string I get the error " Given final block not properly padded" .

I have looked into encryptedString and to-be-decryptedString (using System.out.println statements). They seem identical. String length is correct (multiple of 8). I am frustrated. Can someone please help ??? Thanks.

I generated a Key object and saved it in a file. I am using the same Key for both encryption and decryption.


import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;
import org.bouncycastle.util.encoders.*;



public class CipherExample {

public static void main(String args[]) {
ObjectOutputStream out;
ObjectInputStream in;
String s1 = null;
Key key1 = null;
File Keyfile = new File("KeyObject.txt");
File Stringfile = new File("StringObject.txt");
IvParameterSpec Iv1 = new IvParameterSpec (new byte[] {12, 34, 56, 78, 90, 87, 65, 43});

if (args.length < 2)
System.exit(0);

if (args[0].equals("encrypt")) {
try {
if ( Keyfile.exists()) {
in = new ObjectInputStream(new FileInputStream(Keyfile) );
key1 = (Key) in.readObject();
in.close();
}
Cipher cipher1 = Cipher.getInstance("DES/CBC/PKCS5Padding");
byte[] inputBytes = args[1].getBytes("ISO-8859-1");
cipher1.init(Cipher.ENCRYPT_MODE, key1, Iv1);

byte[] encryptedData = cipher1.doFinal(inputBytes);
String encryptedString = new String(encryptedData,"ISO-8859-1");

out = new ObjectOutputStream(new FileOutputStream(Stringfile) );
out.writeObject(encryptedString);
out.flush();

}
catch (Exception e) {
System.out.println(e);
}
}

if (args[0].equals("decrypt")) {
try {
if ( Keyfile.exists()) {
in = new ObjectInputStream(new FileInputStream(Keyfile) );
key1 = (Key) in.readObject();
in.close();
}
if ( Stringfile.exists()) {
in = new ObjectInputStream(new FileInputStream(Stringfile) );
s1 = (String) in.readObject();
in.close();
}

Cipher cipher1 = Cipher.getInstance("DES/CBC/PKCS5Padding");
byte[] inputBytes = s1.getBytes("ISO-8859-1");

cipher1.init(Cipher.DECRYPT_MODE, key1, Iv1);
byte[] decryptedData = cipher1.doFinal(inputBytes);
String decryptedString = new String(decryptedData,"ISO-8859-1");

System.out.println("\n decrypted string : " + decryptedString);

}
catch (Exception e) {
System.out.println(e);
}
}

}

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 21 2003
Added on Jun 22 2003
5 comments
593 views