哈罗,
我使用Blowfish在Java中进行加密和解密。
加密工作正常,但解密失败。
以下是我用于解密的Java代码:
String encryptedString = … ;
String decryptedString = null;
SecretKeySpec key = new SecretKeySpec(myKey.getBytes(), "Blowfish");
Cipher cipher;
try {
cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decrypted = cipher.doFinal(encryptedString.getBytes());
decryptedString = new String(decrypted, Charset.forName("UTF-8"));
} [ catch Exceptions … ]我得到了一个异常:
Exception. javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
你能告诉我如何让它简单地工作吗?谢谢。
我给出的输入来自我的加密Java代码,+编码在Base64中,并且我在将它提供给这个解密操作之前从Base64解码它。
发布于 2013-04-11 23:27:20
现在我有解决方案了!
首先,Unicode有一些问题,所以我把ISO-8859-1放在任何地方。Including in the Base64 encoding and decoding.
然后,我处理了一些变种。
下面是我的Java代码,它可以用于Blowfish解密:
String encryptedString = … ;
String decryptedString = null;
SecretKeySpec key = new SecretKeySpec(myKey.getBytes(CHARSET_ISO_8859_1), "Blowfish");
Cipher cipher;
try {
cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decrypted = cipher.doFinal(encryptedString.getBytes(CHARSET_ISO_8859_1));
decryptedString = new String(decrypted, CHARSET_ISO_8859_1);
} [ catch Exceptions … ]注意,我已经用"Blowfish/ECB/PKCS5Padding"替换了"Blowfish"来获取Cipher实例,但是,如果您对密钥执行相同的操作,它将失败。
myKey的关键字必须是8个字符的拉丁-1字符串。这就产生了一个128位的密钥。Blowfish算法允许更大的密钥,但由于JRE中的美国出口限制,它们在Java中失败了-美国允许加密,但不会比NSA可以破解的更强。
CHARSET_ISO_8859_1是一个常量,定义如下:
final Charset CHARSET_ISO_8859_1 = Charset.forName("ISO-8859-1");Charset是java.nio.charset.Charset。
最后但同样重要的是,我已经相应地更改了我的加密Java代码。
发布于 2013-04-11 21:16:47
在字节和十六进制之间来回转换是很棘手的。这应该可以解决您的问题。(您需要修复encryptedString的字符串表示)
输出:
StackOverflow 537461636B4F766572666C6F77 [83, 116, 97, 99, 107, 79, 118, 101, 114, 102, 108, 111, 119]
J~3¹ÙÂÖ"¢ª„¨u 194A7E33B9060CD9C2D622A2AA84A875 [25, 74, 126, 51, -71, 6, 12, -39, -62, -42, 34, -94, -86, -124, -88, 117]
StackOverflow 537461636B4F766572666C6F77 [83, 116, 97, 99, 107, 79, 118, 101, 114, 102, 108, 111, 119]代码:
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class Main {
public static void main(String[] args) throws Exception {
KeyGenerator keygenerator = KeyGenerator.getInstance("Blowfish");
SecretKey secretkey = keygenerator.generateKey();
String plaintextString = "StackOverflow";
System.out.println(plaintextString + " " + bytesToHex(plaintextString.getBytes()) + " " + Arrays.toString(plaintextString.getBytes()));
SecretKeySpec key = new SecretKeySpec(secretkey.getEncoded(), "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encrypted = cipher.doFinal(plaintextString.getBytes());
String encryptedString = bytesToHex(encrypted);
System.out.println(new String(encrypted) + " " + encryptedString + " " + Arrays.toString(encrypted));
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decrypted = cipher.doFinal(hexToBytes(encryptedString));
String decryptedString = bytesToHex(decrypted);
System.out.println(new String(decrypted) + " " + decryptedString + " " + Arrays.toString(decrypted));
}
public static byte[] hexToBytes(String str) {
if (str == null) {
return null;
} else if (str.length() < 2) {
return null;
} else {
int len = str.length() / 2;
byte[] buffer = new byte[len];
for (int i = 0; i < len; i++) {
buffer[i] = (byte) Integer.parseInt(str.substring(i * 2, i * 2 + 2), 16);
}
return buffer;
}
}
public static String bytesToHex(byte[] data) {
if (data == null) {
return null;
} else {
int len = data.length;
String str = "";
for (int i = 0; i < len; i++) {
if ((data[i] & 0xFF) < 16)
str = str + "0" + java.lang.Integer.toHexString(data[i] & 0xFF);
else
str = str + java.lang.Integer.toHexString(data[i] & 0xFF);
}
return str.toUpperCase();
}
}
}发布于 2013-04-11 20:30:07
myKey变量长度必须是8的倍数
https://stackoverflow.com/questions/15948662
复制相似问题