Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
praaska
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read the important administrative private message which I have just sent you.
 
Carey Evans
Ranch Hand
Posts: 225
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, this:

should probably read

since that's the encoding used by the encryption.

And make sure to read (and heed) your private messages.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that user name is no better. You know how to change it.

CR
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic