Here is a problem I am facing with BlowFish encryption/Decryption.
The below code is used for testing BlowFish Encryption/Decryption
// Code below omits comments for Brevity
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.math.BigInteger;
public class JBoss {
public static void main(String[] args) throws Exception {
if ((args.length != 2)
|| !(args[0].equals("-e") | args[0].equals("-d"))) {
System.out
.println("Usage:\n\tjava JBoss <-e|-d> <encrypted_password>");
return;
}
String mode = args[0];
byte[] kbytes = "jaas is the way".getBytes();
SecretKeySpec key = new SecretKeySpec(kbytes, "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish");
String out = null;
if (mode.equals("-e")) {
String secret = args[1];
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encoding = cipher.doFinal(secret.getBytes());
out = new BigInteger(encoding).toString(16);
} else {
BigInteger secret = new BigInteger(args[1], 16);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] encoding = cipher.doFinal(secret.toByteArray());
out = new String(encoding);
}
System.out.println(out);
}
}
Now, if I try to encrypt the string
u7mzqw2
I get the value as
-7ccb7ff0c2858a
If I try to decrypt
-7ccb7ff0c2858a
I get the error as below:
java JBoss -d -7ccb7ff0c2858a
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.BlowfishCipher.engineDoFinal(DashoA13*..)
at javax.crypto.Cipher.doFinal(DashoA13*..)
at JBoss.main(JBoss.java:41)
Entire code is here
It has nothing to do with 7 character length original or non /8=0 encrypted value, if I am not mistaken, as the below one works
java JBoss -e qwerty
-40e961f375c2eee6
java JBoss -d -40e961f375c2eee6
qwerty
What am I missing??
BigInteger
? – CodesInChaos