javax.crypto.BadPaddingException Given final block not properly padded?

javax.crypto.BadPaddingException Given final block not properly padded?请添加图片描述

问题描述

最近在玩Cipher加密解密的时候,加密没错,但在解密是抛出错误:

出错描述:提供的字块不符合填补的,解密出错

出错原因DES加密的时候,最后一位长度不足块长64时的,它会自动填补到64,那么在进行字节数组到字串的转化过程中,可以把它填补的不可见字符改变了,所以引发系统抛出异常(若是对称加密当加密与解密的密钥使用的不同,也会出现该异常)

问题处理

源代码(加密解密)

public static void encryptData(String plaintext) {
    System.out.println("-------Encrypting data using AES algorithm-------");
    try {
        byte[] plaintTextByteArray = plaintext.getBytes("UTF8");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, getKey(strKey));
        byte[] cipherText = cipher.doFinal(plaintTextByteArray);
        System.out.println("Original data: " + plaintext);
        System.out.println("Encrypted data:");
        for (int i = 0; i < cipherText.length; i++) {
            System.out.print(cipherText[i] + " ");
        }
        BASE64Encoder encoder = new BASE64Encoder();
        String encoded = encoder.encode(cipherText);
        decryptData(encoded);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

public static void decryptData(String cipherText) {
    System.out.println();
    System.out.println("-------Decrypting data using AES algorithm-------");
    try {
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] c = decoder.decodeBuffer(cipherText);

        System.out.println("Encrypted data:" + cipherText);
        Cipher cipher = Cipher.getInstance("AES");
        Key key = setKey(strKey);
        cipher.init(Cipher.DECRYPT_MODE, getKey(strKey));
        byte[] bytes = cipher.doFinal(c);

        System.out.println("Original data: " + new String(bytes));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
 private static Key getKey(String strKey) throws NoSuchAlgorithmException {
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(128);
        SecretKey secretKey = keyGenerator.generateKey();
        byte[] enCodeFormat = secretKey.getEncoded();
        return new SecretKeySpec(enCodeFormat, "AES");
    }

问题处理:

需要修改keyGenerator的init方法,指定SecurityRandom

private static Key getKey(String strKey) throws NoSuchAlgorithmException {

    KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
    // 指定SecureRandom,使用指定的种子字节播种(需要保证两次调用的strKey不变)
    keyGenerator.init(128 ,new SecureRandom(strKey.getBytes()));
    // keyGenerator.init(128 ,new SecureRandom()); // 这样是不行的
    SecretKey secretKey = keyGenerator.generateKey();
    byte[] enCodeFormat = secretKey.getEncoded();

    return new SecretKeySpec(enCodeFormat, "AES");
}
// 或者(一个道理)
private static Key getKey(String strKey) throws NoSuchAlgorithmException {
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        random.setSeed(strKey.getBytes());
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(128 ,random);
        SecretKey secretKey = keyGenerator.generateKey();
        byte[] enCodeFormat = secretKey.getEncoded();

        return new SecretKeySpec(enCodeFormat, "AES");
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!这个错误提示是Java加密库中的异常之一,表示在解密过程中发生了填充错误,通常是因为密钥或数据被篡改导致的。具体原因需要根据代码和上下文进行分析。 如果您在使用Java加密库进行加密解密操作时遇到了这个异常,建议您检查以下几个方面: 1. 密钥是否正确:请确认您在加密和解密时使用的是同一个密钥,且密钥没有被修改或损坏。 2. 数据\u95ee\u9898\u662f:javax.crypto.badpaddingexception,你上文的回答是:您好!这个错误提示是Java加密库中的异常之一,表示在解密过程中发生了填充错误,通常是因为密钥或数据被篡改导致的。具体原因需要根据代码和上下文进行分析。 如果您在使用Java加密库进行加密解密操作时遇到了这个异常,建议您检查以下几个方面: 1. 密钥是否正确:请确认您在加密和解密时使用的是同一个密钥,并且密钥没有被修改或损坏。 2. 数据是否完整:请检查您要解密的数据是否完整且未被篡改。如果数据被篡改,可能会导致解密时填充错误。 3. 数据是否使用正确的编码格式:如果您在对数据进行编码和解码时使用了不同的格式,也可能会导致填充错误。请确保在加密和解密时使用相同的编码格式。 如果您仍然无法解决问题,建议您提供更多代码和上下文信息,以便我们更好地帮助您分析和解决问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值