Error while Decrypting the String

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • prasath03
    New Member
    • Jun 2007
    • 30

    Error while Decrypting the String

    Hi,

    I wrote a java program for Encrypt and Decrypt the given string, when i execute the program it show me an error....but the string has Encrypted, if i want to Decrypt the string it show me an error...

    The error is:

    javax.crypto.Il legalBlockSizeE xception: Input length must be multiple of 8 when decrypting with padded cipher

    Encoded: --> pLqxYQViK5U
    Decoded: --> null



    what i did wrong in this code...
    i posted my code here...

    Code:

    import java.io.*;
    import java.security.* ;
    import java.util.*;
    import javax.crypto.*;
    import javax.crypto.sp ec.*;
    public class CryptoLibrary
    {
    public static void main(String[] args)

    {
    Cipher encryptCipher;

    Cipher decryptCipher;
    sun.misc.BASE64 Encoder encoder = new sun.misc.BASE64 Encoder();
    sun.misc.BASE64 Decoder decoder = new sun.misc.BASE64 Decoder();
    java.security.S ecurity.addProv ider(new com.sun.crypto. provider.SunJCE ());
    char[] pass = "A".toCharArray ();
    byte[] salt = {

    (byte) 0xa3, (byte) 0x21, (byte) 0x24, (byte) 0x2c,

    (byte) 0xf2, (byte) 0xd2, (byte) 0x3e, (byte) 0x19};
    int iterations = 3;
    String encoded,decoded =null;
    try
    {
    PBEParameterSpe c ps = new javax.crypto.sp ec.PBEParameter Spec(salt, 20);
    SecretKeyFactor y kf = SecretKeyFactor y.getInstance(" PBEWithMD5AndDE S");
    SecretKey k = kf.generateSecr et(new javax.crypto.sp ec.PBEKeySpec(p ass));
    encryptCipher = Cipher.getInsta nce("PBEWithMD5 AndDES/CBC/PKCS5Padding");
    encryptCipher.i nit(Cipher.ENCR YPT_MODE, k, ps);
    decryptCipher = Cipher.getInsta nce("PBEWithMD5 AndDES/CBC/PKCS5Padding");
    decryptCipher.i nit(Cipher.DECR YPT_MODE, k, ps);
    }
    catch (Exception e)
    {
    throw new SecurityExcepti on("Could not initialize CryptoLibrary: " + e.getMessage()) ;
    }
    try
    {
    byte[] utf8 = "Arjunan".getBy tes("UTF8");
    byte[] enc = encryptCipher.d oFinal(utf8);
    encoded=encoder .encode(enc);
    }
    catch (Exception e)
    {
    throw new SecurityExcepti on("Could not encrypt: " + e.getMessage()) ;
    }
    try
    {
    byte[] dec = decoder.decodeB uffer("Arjunan" );
    byte[] utf8 = decryptCipher.d oFinal(dec);
    System.out.prin tln("SSSSSSSSSS SSS ");
    decoded= new String(utf8, "UTF8");
    }
    catch (Exception e)
    {
    System.out.prin tln("Error in decrypting the string "+e);
    }
    try
    {
    System.out.prin tln("Encoded: --> " + encoded);
    System.out.prin tln("Decoded: --> " + decoded);

    }
    catch (Exception e)
    {
    System.out.prin tln("Error in last line of the prg "+e);
    }
    }
    }


    if anyone get the answer... Let me know...


    Thanks in Advance,

    V. Prasath
    Last edited by prasath03; Aug 13 '08, 12:46 PM. Reason: Speilling Mistake
  • cordeo
    New Member
    • Jul 2008
    • 16

    #2
    You feed the original string into the decoder, not the encoded string:
    Code:
                byte[] dec = decoder.decodeBuffer("Arjunan");
    That should of course be:

    Code:
                byte[] dec = decoder.decodeBuffer(encoded);
    which solves your problem immediately.

    Comment

    • blazedaces
      Contributor
      • May 2007
      • 284

      #3
      Also from now on please post your code in code tags (look to the top right corner when you're replying for instructions).

      It makes code a lot more readable (like adding indentations, etc.)

      -blazed

      Comment

      Working...