470,848 Members | 2,040 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,848 developers. It's quick & easy.

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.IllegalBlockSizeException: 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.spec.*;
public class CryptoLibrary
{
public static void main(String[] args)

{
Cipher encryptCipher;

Cipher decryptCipher;
sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
java.security.Security.addProvider(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
{
PBEParameterSpec ps = new javax.crypto.spec.PBEParameterSpec(salt, 20);
SecretKeyFactory kf = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey k = kf.generateSecret(new javax.crypto.spec.PBEKeySpec(pass));
encryptCipher = Cipher.getInstance("PBEWithMD5AndDES/CBC/PKCS5Padding");
encryptCipher.init(Cipher.ENCRYPT_MODE, k, ps);
decryptCipher = Cipher.getInstance("PBEWithMD5AndDES/CBC/PKCS5Padding");
decryptCipher.init(Cipher.DECRYPT_MODE, k, ps);
}
catch (Exception e)
{
throw new SecurityException("Could not initialize CryptoLibrary: " + e.getMessage());
}
try
{
byte[] utf8 = "Arjunan".getBytes("UTF8");
byte[] enc = encryptCipher.doFinal(utf8);
encoded=encoder.encode(enc);
}
catch (Exception e)
{
throw new SecurityException("Could not encrypt: " + e.getMessage());
}
try
{
byte[] dec = decoder.decodeBuffer("Arjunan");
byte[] utf8 = decryptCipher.doFinal(dec);
System.out.println("SSSSSSSSSSSSS ");
decoded= new String(utf8, "UTF8");
}
catch (Exception e)
{
System.out.println("Error in decrypting the string "+e);
}
try
{
System.out.println("Encoded: --> " + encoded);
System.out.println("Decoded: --> " + decoded);

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


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


Thanks in Advance,

V. Prasath
Aug 13 '08 #1
2 4341
cordeo
16
You feed the original string into the decoder, not the encoded string:
Expand|Select|Wrap|Line Numbers
  1.             byte[] dec = decoder.decodeBuffer("Arjunan");
  2.  
That should of course be:

Expand|Select|Wrap|Line Numbers
  1.             byte[] dec = decoder.decodeBuffer(encoded);
  2.  
which solves your problem immediately.
Aug 29 '08 #2
blazedaces
284 100+
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
Aug 29 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Piotr Sawuk | last post: by
2 posts views Thread by Dave Bailey | last post: by
2 posts views Thread by DazedAndConfused | last post: by
4 posts views Thread by Protoman | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.