[英]Java Blowfish CBC Decryption javax.crypto.BadPaddingException: pad block corrupted
I am trying to decrypt bytes using the Blowfish CBC algorithm.我正在尝试使用 Blowfish CBC 算法解密字节。 The encrypted data is hex encoded.加密数据采用十六进制编码。
String decrypt(String skey, String encryptedData) {
byte[] IV = hexStringToByteArray("0001020304050607");
byte[] data = hexStringToByteArray(encryptedData);
try {
SecretKeySpec key = new SecretKeySpec(skey.getBytes("UTF-8"), "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key, new javax.crypto.spec.IvParameterSpec(IV));
byte[] decrypted = cipher.doFinal(data);
return bytesToHex(decrypted);
} catch (Exception e) {
return e.toString();
}
}
It returns javax.crypto.BadPaddingException: pad block corrupted, null
(the errors are returned for debugging/testing purproses).它返回javax.crypto.BadPaddingException: pad block corrupted, null
blockcorruption javax.crypto.BadPaddingException: pad block corrupted, null
(返回错误用于调试/测试目的)。 Thank you.谢谢你。
import javax.crypto.spec.SecretKeySpec;
import static javax.xml.bind.DatatypeConverter.*;
public class BlowFishTest {
private String encrypt(String skey, String unencryptedData) {
byte[] IV = hexStringToByteArray("0001020304050607");
byte[] data = hexStringToByteArray(unencryptedData);
try {
SecretKeySpec key = new SecretKeySpec(skey.getBytes("UTF-8"), "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key,
new javax.crypto.spec.IvParameterSpec(IV));
byte[] encrypted = cipher.doFinal(data);
return bytesToHex(encrypted);
} catch (Exception e) {
return e.toString();
}
}
private String decrypt(String skey, String encryptedData) {
byte[] IV = hexStringToByteArray("0001020304050607");
byte[] data = hexStringToByteArray(encryptedData);
try {
SecretKeySpec key = new SecretKeySpec(skey.getBytes("UTF-8"), "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key, new javax.crypto.spec.IvParameterSpec(IV));
byte[] decrypted = cipher.doFinal(data);
return bytesToHex(decrypted);
} catch (Exception e) {
return e.toString();
}
}
public byte[] hexStringToByteArray(String encryptedData) {
return parseHexBinary(encryptedData);
}
public String bytesToHex(byte[] decrypted) {
return printHexBinary(decrypted);
}
public static void main(String[] args) {
String myKey = "mySecret";
String message = "myTestingString";
BlowFishTest blowFishTest = new BlowFishTest();
String myHexString = blowFishTest.bytesToHex(message.getBytes());
String encryptedHex = blowFishTest.encrypt(myKey, myHexString);
String unencryptedHex = blowFishTest.decrypt(myKey, encryptedHex);
System.out.println(new String(blowFishTest.hexStringToByteArray(unencryptedHex)).equals(message));
}
}
true
Unfortunately I am unable to reproduce the issue with the codes you have provided.不幸的是,我无法使用您提供的代码重现该问题。 You may need to check your input, which is unencryptedData and the hexStringToByteArray and bytesToHex methods.您可能需要检查您的输入,即 unencryptedData 和 hexStringToByteArray 和 bytesToHex 方法。
My mistake.我的错。 It should be Blowfish/CBC/NoPadding
应该是Blowfish/CBC/NoPadding
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.