这是我在使用 BlowFish 加密/解密时遇到的问题。
以下代码用于测试 BlowFish 加密/解密
// 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);
}
}
现在,如果我尝试加密字符串
u7mzqw2
我得到的价值为
-7ccb7ff0c2858a
如果我尝试解密
-7ccb7ff0c2858a
我得到如下错误:
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)
完整代码在这里
如果我没记错的话,它与 7 个字符长度的原始值或非 /8=0 加密值无关,如下所示
java JBoss -e qwerty
-40e961f375c2eee6
java JBoss -d -40e961f375c2eee6
qwerty
我错过了什么??