javax.crypto.BadPaddingException - 使用Salt和IV对AES256进行解密

时间:2018-01-30 16:01:23

标签: java encryption cryptography aes salt

我使用带有salt和IV的AES来加密和解密一个唯一的ID,但是在解密时它给出了javax.crypto.BadPaddingException。

解密数据时每次都给出完整的错误堆栈跟踪

javax.crypto.BadPaddingException: Given final block not properly padded null


at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:991)
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:847)
    at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446)
    at javax.crypto.Cipher.doFinal(Cipher.java:2165)
    at com.data.commons.security.impl.DataAESCrypt.decode(DataAESCrypt.java:84)
    at com.data.CryptoTest.main(CryptoTest.java:13)

加密方法 -

private static final int PASSWORD_ITERATIONS = 65536;
private static final int KEY_LENGTH          = 256;
private static byte[] salt = new byte[16];
private static byte[] iv= new byte[16];
private static final String ALGORITHM = "AES/CBC/PKCS5Padding" ;


@Override
public String encode(String plainText) throws Exception {
    // TODO Auto-generated method stub
       try {
            SecureRandom random = new SecureRandom();

            random.nextBytes(salt);

            SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
            KeySpec spec = new PBEKeySpec(plainText.toCharArray(), salt, PASSWORD_ITERATIONS, KEY_LENGTH);
            SecretKey tmp = factory.generateSecret(spec);
            SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");

            Cipher cipher = Cipher.getInstance(ALGORITHM);
            AlgorithmParameters params = cipher.getParameters();
             iv = params.getParameterSpec(IvParameterSpec.class).getIV();
             cipher.init(Cipher.ENCRYPT_MODE, secret);
            byte[] encryptedText = cipher.doFinal(plainText.getBytes("UTF-8"));


            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            outputStream.write(salt);
            outputStream.write(iv);
            outputStream.write(encryptedText);

            System.out.println("Salt " + DatatypeConverter.printBase64Binary(salt) + " IV " + DatatypeConverter.printBase64Binary(iv) );

            return DatatypeConverter.printBase64Binary(outputStream.toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

}

解密方法

public String decode(String encodedText) throws Exception {
    // TODO Auto-generated method stub
    try {
        byte[] ciphertext = DatatypeConverter.parseBase64Binary(encodedText);
        if (ciphertext.length < 48) {
            return null;
        }
        byte[] salt = Arrays.copyOfRange(ciphertext, 0, 16);
        byte[] iv = Arrays.copyOfRange(ciphertext, 16, 32);
        byte[] ct = Arrays.copyOfRange(ciphertext, 32, ciphertext.length);

        System.out.println("Salt " + DatatypeConverter.printBase64Binary(salt) + " IV " + DatatypeConverter.printBase64Binary(iv) );
        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        KeySpec spec = new PBEKeySpec(encodedText.toCharArray(), salt, PASSWORD_ITERATIONS, KEY_LENGTH);
        SecretKey tmp = factory.generateSecret(spec);
        SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
        Cipher cipher = Cipher.getInstance(ALGORITHM);

        cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv));
        byte[] plaintext = cipher.doFinal(ct);

        return new String(plaintext, "UTF-8");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;

}

我是JCA的新手。

1 个答案:

答案 0 :(得分:1)

您在plainText.toCharArray()中使用encodedText.toCharArray()PBEKeySpec作为密码。相反,请使用实际的密码。目前,只有在您知道明文消息时才能获取纯文本消息,这并不是很有用。用编码的密文作为输入对其进行解密肯定不起作用。