URGENT: IllegalBlockSizeException Please Help
Hi
I am using DES/ECB/PKCS5Padding to generate a key. I am storing the key in a file. I am using the key to encrypt the password, which I store in a password field (size: varchar(128) in Oracle). When I decrypt the password at the time of login (using the key from the file), I get the IllegalBlockSizeException.
The curious part is that the password decryption works fine for password sizes of 7 character or less!
I have even reinstalled the jce1.2.1 package.
My code is:
/**
* Encrypting the String
*
*/
private String encrypt(String password)
{
String passwd = null;
String pass1 = null;
try
{
SecretKey desKey = null;
Provider sunJCE = new com.sun.crypto.provider.SunJCE();
Security.addProvider(sunJCE);
try {
ObjectInputStream object1 = new ObjectInputStream(new FileInputStream(certificate));
desKey = (SecretKey) object1.readObject();
object1.close();
System.out.println("Using the existing Certificate File");
}
catch (Exception e)
{
System.out.println("The Certificate File does not Exist. So, it is being created.");
KeyGenerator keygen = KeyGenerator.getInstance("DES");
keygen.init(new SecureRandom());
desKey = keygen.generateKey();
OutputStream f0 = new FileOutputStream(certificate);
ObjectOutputStream object0 = new ObjectOutputStream(f0);
object0.writeObject(desKey);
object0.close();
// writing the secretkey to a file
Runtime.getRuntime().exec("cp "+certificate+" "+bkpcertificate);
}
Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
// Initialize the cipher for encryption
desCipher.init(Cipher.ENCRYPT_MODE, desKey);
// Encrypt the cleartext
byte[] ciphertext = desCipher.doFinal(password.getBytes("UTF8"));
passwd = new String(ciphertext);
}
catch (java.io.FileNotFoundException e) { e.printStackTrace(); }
catch (java.io.IOException e) { e.printStackTrace(); }
catch (java.security.InvalidKeyException e) { e.printStackTrace(); }
catch (java.security.NoSuchAlgorithmException e) { e.printStackTrace(); }
catch (javax.crypto.IllegalBlockSizeException e) { e.printStackTrace(); }
catch (javax.crypto.BadPaddingException e) { e.printStackTrace(); }
catch (javax.crypto.NoSuchPaddingException e) { e.printStackTrace(); }
return passwd;
}
-
for decrypting
-
/**
* Decrypting the String
*
*/
private String decrypt(String password)
{
String passwd = null;
try
{
Provider sunJCE = new com.sun.crypto.provider.SunJCE();
Security.addProvider(sunJCE);
ObjectInputStream object1 = new ObjectInputStream(new FileInputStream(certificate));
SecretKey desKey = (SecretKey) object1.readObject();
object1.close();
Cipher desCipher = Cipher.getInstance("DES");
// Initialize the cipher for encryption
desCipher.init(Cipher.DECRYPT_MODE,desKey);
// Encrypt the cleartext
byte[] ciphertext = desCipher.doFinal(password.getBytes());
passwd = new String(ciphertext);
}
catch (java.io.FileNotFoundException e) { System.out.println("ERROR!!! YOUR CERTIFICATE FILE IS INACCESSIBLE TO DECRYPT SENSITIVE INFORMATION. YOU MAY NEED TO RESTORE IT FROM THE ALTERNATE BACKUP.");
e.printStackTrace(); }
catch (java.io.StreamCorruptedException e) { e.printStackTrace(); }
catch (java.io.IOException e) { e.printStackTrace(); }
catch (java.lang.ClassNotFoundException e) { e.printStackTrace(); }
catch (java.security.InvalidKeyException e) { e.printStackTrace(); }
catch (java.security.NoSuchAlgorithmException e) { e.printStackTrace(); }
catch (javax.crypto.IllegalBlockSizeException e) { e.printStackTrace(); }
catch (javax.crypto.BadPaddingException e) { e.printStackTrace(); }
catch (javax.crypto.NoSuchPaddingException e) { e.printStackTrace(); }
return passwd;
}
the error is
javax.crypto.IllegalBlockSizeException: Input length (with padding) not multiple of 8 bytes

