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

时间:2019-04-20 17:10:45

标签: java encryption blowfish

我正在尝试使用Blowfish CBC算法解密字节。加密的数据是十六进制编码的。

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();
  }
}

它返回javax.crypto.BadPaddingException: pad block corrupted, null(错误将返回给调试/测试钱包)。谢谢。

2 个答案:

答案 0 :(得分:-1)

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

很遗憾,我无法使用您提供的代码来重现该问题。您可能需要检查输入内容,即unencryptedData以及hexStringToByteArray和bytesToHex方法。

答案 1 :(得分:-1)

我的错误。应该是{ "_id" : ObjectId("57506d74c469888f0d631be6"), "financials" : "{"year":[2015], ...}" }