File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Exception while decrypting the string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Exception while decrypting the string" Watch "Exception while decrypting the string" New topic
Author

Exception while decrypting the string

Shital Joshi
Greenhorn

Joined: Jun 30, 2010
Posts: 9
Hi,
here are routines i have written for encryption and decryption of string using key k, its giving me execption as

javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipherdrd

please help me out, to find error.
thanks in advance.

public static String chat_encrypt(String in,BigInteger k)
{
try
{

// Security.addProvider(new BouncyCastleProvider());
DHPrivateKeySpec d=new DHPrivateKeySpec(k,pg.p,new BigInteger("2"));
DHPublicKeySpec d1=new DHPublicKeySpec((new BigInteger("2")).modPow(k,pg.p),pg.p,new BigInteger("2"));
KeyFactory keyFactory = KeyFactory.getInstance("DH");
PrivateKey bob = (DHPrivateKey) keyFactory.generatePrivate(d);
PublicKey bob1 = (DHPublicKey) keyFactory.generatePublic(d1);
KeyAgreement bobKeyAgree = KeyAgreement.getInstance("DH");
bobKeyAgree.init(bob);
bobKeyAgree.doPhase(bob1, true);
SecretKey bobby = bobKeyAgree.generateSecret("DES");
// System.out.println(bobby+"encrypt");
Cipher desCipher = Cipher.getInstance("DES");
desCipher.init(Cipher.ENCRYPT_MODE, bobby);

byte[] cleartext = in.getBytes("UTF8");
byte[] ciphertext = desCipher.doFinal(cleartext);
// System.out.println(cleartext.length+"<<>>"+ciphertext.length+"<<>>"+in+" "+in.length()+" "+new String(ciphertext)+">>");
return new sun.misc.BASE64Encoder().encode(ciphertext);

}catch(Exception e){System.out.println(e+"drd");}
return "Error encrypting";
}

public static String chat_decrypt(String in,BigInteger k)
{
try
{
// Security.addProvider(new BouncyCastleProvider());
DHPrivateKeySpec d=new DHPrivateKeySpec(k,pg.p,new BigInteger("2"));
DHPublicKeySpec d1=new DHPublicKeySpec((new BigInteger("2")).modPow(k,pg.p),pg.p,new BigInteger("2"));
KeyFactory keyFactory = KeyFactory.getInstance("DH");
PrivateKey bob = (DHPrivateKey) keyFactory.generatePrivate(d);
PublicKey bob1 = (DHPublicKey) keyFactory.generatePublic(d1);
KeyAgreement bobKeyAgree = KeyAgreement.getInstance("DH");
bobKeyAgree.init(bob);
bobKeyAgree.doPhase(bob1, true);
SecretKey bobby = bobKeyAgree.generateSecret("DES");
// System.out.println(bobby+"decrypt");
Cipher desCipher = Cipher.getInstance("DES");
desCipher.init(Cipher.DECRYPT_MODE, bobby);

//byte[] ciphertext = in.getBytes("UTF8");
byte[] ciphertext=new sun.misc.BASE64Decoder().decodeBuffer(in);
//System.out.println(ciphertext.length+"<<>>"+in+" "+in.length());
in=new String(ciphertext);
byte[] cleartext = desCipher.doFinal(ciphertext);

return new String(cleartext);

}catch(Exception e){System.out.println(e+"drd");}

return in;
}
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337

Both these statements use the platform default encoding - which probably is not UTF-8 (which you are using elsewhere).
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Shital Java wrote:Hi,
javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipherdrd


This indicates that you have corrupted your ciphertext. Make sure that the ciphertext generated by the encryption method is exactly the same as the ciphertext being decrypted and is of length (with the base64 encoding removed) a multiple of 8 .

Note - I absolutely hate your key generation since although it uses DH to generate a key it does not seem to use DH to agree a shared secret key between the sender and receiver.

Note 1 - your exception handling is at best poor since you lose most of the information (the stack trace) and you try to continue even though the encryption or decryption has failed. You should either just pass the exception back to the caller OR you should convert the exception to an exception of your own (I use an unchecked exception derived from Error since I regard any exception here as a system error and fatal) and pass that back to the caller. You certainly should not continue regardless.

Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Lester Burnham wrote:
Both these statements use the platform default encoding - which probably is not UTF-8 (which you are using elsewhere).


Undoubtedly a likely problem source but the OP's problem is more fundamental even than this - the ciphertext is corrupt.
Shital Joshi
Greenhorn

Joined: Jun 30, 2010
Posts: 9
thanks all

i got the solution

thnks again
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Shital Java wrote:i got the solution

Can you tell everybody what the solution was ?

(the next time you post some code, please UseCodeTags)


[My Blog]
All roads lead to JavaRanch
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Shital Java wrote:thanks all

i got the solution

thnks again


It's nice to get thanks but it would be nicer if this response had indicated that the OP had taken action on my observations and taken appropriate action.
Shital Joshi
Greenhorn

Joined: Jun 30, 2010
Posts: 9
actually my string i was trying to decrypt was not encrypted in one case,

so was getting error.

the code itself is working properly.

thnks
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Shital Java wrote:actually my string i was trying to decrypt was not encrypted in one case,

so was getting error.

the code itself is working properly.

thnks


:-) But, as I pointed out, your code has serious flaws! I have shown you the water but I can't force you to drink!
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Heh, Retired horse trader.
(and I wrote the double underline stuff, sorry)
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

David O'Meara wrote:Heh, Retired horse trader.
(and I wrote the double underline stuff, sorry)


Yep - 45 years man and boy as a horse trader before deciding that enough was enough. I have written my fair share of unpopular code when the business people (who paid my salary) insisted but thankfully I no longer have to.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

James Sabre wrote:I have shown you the water but I can't force you to drink!

James Sabre wrote:Yep - 45 years man and boy as a horse trader before deciding that enough was enough.

I like this
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception while decrypting the string
 
Similar Threads
DHPublicKey not for this KeyAgreement!
javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8
3DES encryption and decryption
Cryptography - deciphering doesn't yield the same text
decryption resulting in BadPaddingException