简体   繁体   English

Java Blowfish CBC 解密 javax.crypto.BadPaddingException:填充块已损坏

[英]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.

相关问题 文件解密失败:javax.crypto.BadPaddingException:填充块已损坏 - Decryption of file fails: javax.crypto.BadPaddingException: pad block corrupted 错误:javax.crypto.BadPaddingException:解密时填充块损坏 - Error : javax.crypto.BadPaddingException: pad block corrupted while Decryption javax.crypto.BadPaddingException:pad块损坏 - javax.crypto.BadPaddingException: pad block corrupted AES128解密:javax.crypto.badpaddingexception pad块损坏 - AES128 Decryption :javax.crypto.badpaddingexception pad block corrupted Android javax.crypto.BadPaddingException:填充块已损坏 - Android javax.crypto.BadPaddingException: pad block corrupted javax.crypto.BadPaddingException:填充块损坏的异常 - javax.crypto.BadPaddingException: pad block corrupted exception javax.crypto.BadPaddingException:填充块有时损坏 - javax.crypto.BadPaddingException: pad block corrupted sometimes Java - 解密:javax.crypto.BadPaddingException - Java - Decryption : javax.crypto.BadPaddingException 函数解密引发javax.crypto.BadPaddingException:android中的类SimpleCrypto中的填充块已损坏 - function decrypt throws javax.crypto.BadPaddingException: pad block corrupted in class SimpleCrypto in android 解密图像时,给出javax.crypto.BadPaddingException:填充块损坏的Android - When Decrypting Image, gives javax.crypto.BadPaddingException: pad block corrupted Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM