Win a copy of Emmy in the Key of Code this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

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

 
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
 
Marshal
Posts: 66135
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read the important administrative private message which I have just sent you.
 
Ranch Hand
Posts: 225
Eclipse IDE Debian 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.
 
Rancher
Posts: 43011
76
  • 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
Marshal
Posts: 66135
250
  • 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
 
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!