Click here to Skip to main content
11,928,732 members (56,174 online)
Rate this:
Please Sign up or sign in to vote.
See more: Java Java SE J2EE

I'm in trouble to decrypt the data which I got from servlet.

MY code is
//getting the content from servlet
 URL url = new URL("http://localhost:8084/DataService/GetData?ChartName=preferences");
        //open url connection
        URLConnection yc = url.openConnection();
        //get value in stream format
        InputStream inputStream = yc.getInputStream();
        byte[] data = new byte[2125];;

  // get the key
  DESKeySpec spec = new DESKeySpec(keyBytes);
        SecretKeyFactory instance = SecretKeyFactory.getInstance("DES");
        SecretKey key = instance.generateSecret(spec);
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] doFinal = cipher.doFinal(data);
        System.out.println(new String(doFinal));
and I'm getting the following error
javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
        at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
        at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
        at com.sun.crypto.provider.DESCipher.engineDoFinal(DashoA13*..)
        at javax.crypto.Cipher.doFinal(DashoA13*..)
        at cbcdemo.Main.main(
thanks in advance
Posted 14-Mar-11 1:39am
Edited 14-Mar-11 1:53am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The error message says it all:
Input length must be multiple of 8 when decrypting with padded cipher
You are providing it with 2125 bytes, which is not a multiple of 8.

When you are ENcrypting, padding is used to make your input up to a multiple of the block size (so the last input block is full size). When you are DEcrypting, you need to decrypt full blocks, then strip off or ignore any padding in the decrypted output. There are a number of techniques for identifying what is "real" data and what is padding.

The whole point is that using DES/ECB, encrypted data comes in blocks of 8 bytes. Plain text can be any length, and any difference is made up by padding during encryption. If you encrypt something, then throw away part of the last block, you'll never recover the last block when you decrypt. So, you'll need to read a multiple of 8 bytes from your servlet.

If you like the answer, vote and accept it.

balajict 14-Mar-11 7:32am
yes i understand :) but my problem is i'm getting the encrypted data from a servlet so how could i define the length of the bytes to store? how do i make the encrypted data much perfect as like i got when i encrypt in servlet? is their any solution to tat?
Peter_in_2780 14-Mar-11 17:50pm
When the data is encrypted in the servlet, you must keep all the last block so you client can read it. Maybe the data is already there and you just need to round-up the length you use (in other words, try reading 2132 bytes to get the 2125 you want.)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web03 | 2.8.151126.1 | Last Updated 14 Mar 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100