这是我面临的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)
整个代码为here
它与7个字符长度的原始或非/ 8 = 0加密值无关,如果我没有记错的话,如下所示
java JBoss -e qwerty
-40e961f375c2eee6
java JBoss -d -40e961f375c2eee6
QWERTY
我错过了什么?
答案 0 :(得分:1)
这是我的观察:我修改了一些代码,添加了几个带有单个字节和相应十六进制值的sops。
输入: asdfda
16 :: 10
60 :: 3C
105 :: 69
57 :: 39
-60 :: - 3C
110 :: 6E
19 :: 13
-52 :: - 34
编码值:103c6939c46e13cc
正如您所看到的那样,左边的项是字节,右边是带有基数16值的单个biginteger,在底部我们有编码值。您可能会看到一个大模式匹配。除了带有-tive的值。像-60对应值-3c,但与1字节的结果一样,值为c4(见yourslef)。
现在我测试了加密为u7mzqw2的值,让我们看看会发生什么。
输入: u7mzqw2
-1 :: - 1
-125 :: - 7D
52 :: 34
-128 :: - 80
15 ::˚F
61 :: 3D
122 :: 7A
118 :: 76
编码值:-7ccb7ff0c2858a
现在你看到模式匹配,现在你不会,为什么不呢?让我们看看-1,其十六进制应该是-1? ??? ,不,它是0XFF,现在我们可以在字节中表示0xFF吗?不,我们不能。自己读一读Byte and -1
更新:令我困惑的是这些编码是如何评估的?还在寻找,帮我识别一下
答案 1 :(得分:0)
试试这个,
public static void main(String[] args) throws Exception {
// generate key
KeyGenerator keyGen = KeyGenerator.getInstance("Blowfish");
SecretKey secretKey = keyGen.generateKey();
// get Cipher and init it for encryption
Cipher cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
String data="u7mzqw2";
// encrypt data
byte[] cipherText = cipher.doFinal(data.getBytes());
// get the initialization vector from the cipher
byte[] ivBytes = cipher.getIV();
IvParameterSpec iv = new IvParameterSpec(ivBytes);
byte[] keyBytes = secretKey.getEncoded();
// create a SecretKeySpec from key material
SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "Blowfish");
// get Cipher and init it for encryption
cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, iv);
byte[] plainText = cipher.doFinal(cipherText);
System.out.println(new String(plainText));
}
答案 2 :(得分:-1)
如果我们查看secret.toByteArray()中的行后面的代码,则给出长度为7字节的数组,这应该是问题的原因。
byte[] encoding = cipher.doFinal(secret.toByteArray()); //During decoding logic
问题似乎出现在BlowfishCipher,CipherCore。
的实施中public BlowfishCipher()
{
core = new CipherCore(new BlowfishCrypt(),
BlowfishConstants.BLOWFISH_BLOCK_SIZE);
}
http://www.docjar.com/html/api/com/sun/crypto/provider/BlowfishConstants.java.html
我们正在使用BlowfishConstants.BLOWFISH_BLOCK_SIZE = 8; //字节数
if ((paddingLen > 0) && (paddingLen != blockSize) &&
(padding != null) && decrypting) {
throw new IllegalBlockSizeException
("Input length must be multiple of " + blockSize +
"when decrypting with padded cipher");
}