• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception while decrypting the string

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Both these statements use the platform default encoding - which probably is not UTF-8 (which you are using elsewhere).
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks all

i got the solution

thnks again
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shital Java wrote:i got the solution


Can you tell everybody what the solution was ?

(the next time you post some code, please UseCodeTags)
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Heh, Retired horse trader.
(and I wrote the double underline stuff, sorry)
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic