gpt4 book ai didi

java - 使用 Blowfish 在 Java 中解密

转载 作者:行者123 更新时间:2023-12-03 20:25:24 25 4
gpt4 key购买 nike

你好,

我正在使用 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:使用填充密码解密时输入长度必须是 8 的倍数

你能告诉我如何让它简单地工作吗?谢谢。

我提供的输入来 self 的加密 Java 代码,+ Base64 编码,我在将其提供给此解密操作之前从 Base64 对其进行解码。

最佳答案

将字节转换为十六进制并返回是很棘手的。这应该可以解决您的问题。 (您需要修复 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();
}
}
}

关于java - 使用 Blowfish 在 Java 中解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15948662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com