javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8
843811Dec 28 2007 — edited Mar 9 2009A VERY GOOD AFTRERNOON TO ALL EXPERTS
i am getting problem during decryption of blowfish
my code as follows:
Hading6:h6.
import java.io.*;
import java.security.*;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.*;
import java.math.*;
public class BlowfishCipher {
public static void main(String[] args) throws Exception
{
System.out.println("enter ur key");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String key=in.readLine();
String plainText="surendra";
byte[] raw=key.getBytes("UTF8");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] pText=plainText.getBytes("UTF8");
byte[] encrypted = cipher.doFinal(pText);
String hexString = getHexString(encrypted);
System.out.println(hexString);
System.out.println("for decryption:");
decrypt(hexString);
System.exit(0);
}
public static String getHexString(byte[] b) throws Exception
{
String result = "";
for (int i=0; i < b.length; i++)
{
result +=
Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
}
return result;
}
public static void decrypt(String cipherText) throws Exception
{
System.out.println("enter ur key");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String key=in.readLine();
// create a key
byte[] raw=key.getBytes("UTF8");
byte[] cText = new BigInteger(cipherText,16).toByteArray();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(cText);
String s=new String(decrypted, "UTF8");
System.out.println(s);
}
}
output
enter ur key:surendra12
a8786c3a167e022fe741f998f9bbe6a6
for decryption
enter ur key:surendra12
Exception in thread "main" javax.crypto.IllegalBlockSizeException:input length must be multiple of 8 when decrypting with padded cipher
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
at com.sun.crypto.provider.BlofishCipher.engineDoFinal(DashoA13*..)
at com.sun.crypto.doFinal(DashoA13*..)
at com.ack.security.jce.BlofishCipher.main(BlowfishCipher.java:26)
note:
1)its working when we r suppling multiple of 6 characters as key
2)when we r using sun.misc.* BASE64 encoding/decoding its working properly but some environments denying this package(for instance while calling these functions in html pages through applet)