下面是我在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
我遗漏了什么??
发布于 2013-01-23 11:26:22
下面是我的观察:我稍微修改了代码,添加了几个带有单个字节的sops和相应的十六进制值。
输入: asdfda
16:10
60:3
105:69
57:39
-60::-3c
110::6e
19:13
-52::-34
编码值:103c6939c46e13cc
如您所见,左边的项是字节,右边是带有基数16值的单个biginteger,在底部有编码值。你可能会看到一个很大的模式匹配。除了使用-tive的值之外。类似-60对应的值为-3c,但与1字节consersion一样,该值为c4 (请参阅yourslef)。
现在我测试了要加密为u7mzqw2的值,让我们看看会发生什么。
输入: u7mzqw2
-1:-1
-125::-第7天
52::34
-128:-80
15:F
61:3D
122::7a
118::76
编码值:-7ccb7ff0c2858a
现在你看到的模式匹配,现在你不会,为什么不呢?让我们看看,谁的十六进制应该是-1?,不应该是0 0XFF,现在我们可以用Byte来表示0 0xFF吗?不,我们不能。读你自己的字节和-1
更新:令我困惑的是如何评估这些编码?还在找,帮我找出这个
发布于 2013-01-23 15:54:50
尝尝这个,
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));
}发布于 2013-01-23 09:26:59
如果我们查看我们的代码,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;//字节数
http://hg.openjdk.java.net/jdk6/jdk6/jdk/raw-file/2d585507a41b/src/share/classes/com/sun/crypto/provider/CipherCore.java
if ((paddingLen > 0) && (paddingLen != blockSize) &&
(padding != null) && decrypting) {
throw new IllegalBlockSizeException
("Input length must be multiple of " + blockSize +
"when decrypting with padded cipher");
}https://stackoverflow.com/questions/14474669
复制相似问题