Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

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

MY code is
Java
//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];
        inputStream.read(data);
        inputStream.close();


  // get the key
  DESKeySpec spec = new DESKeySpec(keyBytes);
        SecretKeyFactory instance = SecretKeyFactory.getInstance("DES");
        SecretKey key = instance.generateSecret(spec);
        
     //decrypction
        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(Main.java:86)

thanks in advance
Posted
Updated 14-Mar-11 0:53am
v3

1 solution

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.

Peter
 
Share this answer
 
v2
Comments
Balaji_Reddy 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900