Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java J2EE
Hi,
 
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];
        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 14-Mar-11 1:39am
Edited 14-Mar-11 1:53am
v3

1 solution

Rate this: bad
good
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.
 
Peter
  Permalink  
v2
Comments
balajict at 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 at 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
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,382


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 14 Mar 2011
Copyright © CodeProject, 1999-2014
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