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!

....Input length must be multiple of 8 when decrypting with padded cipher..

843811Mar 22 2007 — edited Mar 22 2007
Hi,
Am Writing a code that encrypts data before posting to the database. When i use the same code for file encryption, it works- but not for database.
Here is the code;

public DesEncryption() {
try {
key = KeyGenerator.getInstance("DES").generateKey();
}
catch (NoSuchAlgorithmException e) {
System.out.println("No such algorithm");
}
try {
ecipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
}
catch (javax.crypto.NoSuchPaddingException e) {
System.out.println(e.getMessage());
}
catch (java.security.NoSuchAlgorithmException e) {
System.out.println(e.getMessage());
}

}

public String encrypt(String instring) {
byte[] clearText = instring.getBytes();
byte[] cipherText = null;
String mycipher = " ";
try{
ecipher.init(Cipher.ENCRYPT_MODE, key,ecipher.getParameters());
}
catch (java.security.InvalidKeyException e) {
System.out.println(e.getMessage());
}
catch (InvalidAlgorithmParameterException e) {
System.out.println(e.getMessage());
}

try {
cipherText = ecipher.doFinal(clearText);
mycipher = cipherText.toString();
}
catch (BadPaddingException ex) {
/** @todo Handle this exception */
}
catch (IllegalBlockSizeException ex) {
/** @todo Handle this exception */
}
return mycipher;
}

public String decrypt(String instring) {
byte[] clearText = instring.getBytes();
byte[] cipherText = null;
String mycipher = " ";
try {
ecipher.init(Cipher.DECRYPT_MODE, key, ecipher.getParameters());

}
catch (java.security.InvalidKeyException e) {
System.out.println(e.getMessage());
}
catch (InvalidAlgorithmParameterException e) {
System.out.println(e.getMessage());
}
try {
cipherText = ecipher.doFinal(clearText);
mycipher = cipherText.toString();
}

catch (BadPaddingException ex) {
System.err.println("Bad Padding Exception "+ex.getMessage() );
/** @todo Handle this exception */
}
catch (IllegalBlockSizeException ex) {
System.err.println("Illegal Block Size Exception ....."+ex.getMessage() );
}
return mycipher;
}

the error message is "Input length must be multiple of 8 when decrypting with padded cipher" when i try to decipher.

Any suggestions?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 19 2007
Added on Mar 22 2007
1 comment
423 views