File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Security and the fly likes Java Cryptography Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Security
Bookmark "Java Cryptography "Input length (with padding) not multiple of 8 bytes   "" Watch "Java Cryptography "Input length (with padding) not multiple of 8 bytes   "" New topic
Author

Java Cryptography "Input length (with padding) not multiple of 8 bytes "

praaska
Greenhorn

Joined: Dec 11, 2008
Posts: 1
Hi,
I am new to cryptography. I am trying to encrypt the data using Triple DES algorithm in RAD Version 6.1. But during decrypting it is showing error as "Input length (with padding) not multiple of 8 bytes".

After the getting the instance of Algorithm
keyGenerator = KeyGenerator.getInstance("DESede");
cipher = Cipher.getInstance(DESede);

I have generated the SymmetricKey using keyGenerator.generateKey(); and passing this to encrypt and decrypt methods respectively

ENCRYPT METHOD:

--------------------------------------------------------------------------------

public String encrypt(String data,Key key)
{
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] dataBytes = data.getBytes("ISO-8859-1");
byte[] byteCipherText = cipher.doFinal(dataBytes);
return getHexString(byteCipherText);
}
Based on requirement, I am converting this byteCipherText to Hexadecimal String using the following code

public String getHexString(byte[] buf) {
String result = "";
for (int i=0;i < buf.length; i++) {
result +=
Integer.toString( ( buf[i] & 0xff ) + 0x100, 16).substring( 1 );
}
return result;
}
DECRYPT METHOD: I am converting this hexadecimal string back to byte[] and then decrypting it using the key

--------------------------------------------------------------------------------
public String decrypt(String data,Key key) {

byte[] dataBytes = new BigInteger(data,16).toByteArray();

cipher.init(Cipher.DECRYPT_MODE,key);

byte[] recoveredBytes = cipher.doFinal(dataBytes);

return ( new String(recoveredBytes)) ;
}

When i am printing provider using cipher.getProvider() it's printing IBMJCE version 1.2
I tried to use CBC/PKCS5Padding, but it's not supporting that always showing error as "Input length (with padding) not multiple of 8 bytes"

Can anyone solve this problem

Thanks in Advance
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
Please read the important administrative private message which I have just sent you.
Carey Evans
Ranch Hand

Joined: May 27, 2008
Posts: 225

The BigInteger.toByteArray() method does not do what you expect. If you want to encode and decode a byte array as base 16, have a look at Apache Commons Codec.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41867
    
  63
Also, this:

should probably read

since that's the encoding used by the encryption.

And make sure to read (and heed) your private messages.


Ping & DNS - my free Android networking tools app
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
No, that user name is no better. You know how to change it.

CR
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Cryptography "Input length (with padding) not multiple of 8 bytes "