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