Java中的Aes解密 - 填充问题

时间:2016-05-08 10:44:34

标签: java encryption cryptography aes padding

我想在java中实现一个简单的加密/解密工具。 因此,我找到了一个小教程:http://www.codejava.net/coding/file-encryption-and-decryption-simple-example

我更改了一些代码行,以保证大文件的加密。 现在我遇到了问题,解密不起作用。

我收到以下错误消息/异常:

Error encrypting/decrypting file
    at Algorithmus.Encryptor.doCrypto(Encryptor.java:71)
    at Algorithmus.Encryptor.decrypt(Encryptor.java:39)
    at GUI.MainWindow$encryptThread.run(MainWindow.java:838)
Caused by: javax.crypto.BadPaddingException: Given final block not properly padded
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:966)
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:824)
    at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:436)
    at javax.crypto.Cipher.doFinal(Cipher.java:2165)
    at Algorithmus.Encryptor.doCrypto(Encryptor.java:60)
    ... 2 more

我尝试将Transoformation参数更改为AES/CBC/PKCS5Padding,但这没有任何效果。有谁知道,如何优化给定的代码?

        private static final String ALGORITHM = "AES";

        private static final String TRANSFORMATION = "AES";

        public static  void encrypt(String key, File inputFile, File outputFile)
                throws ExtendedException {
            doCrypto(Cipher.ENCRYPT_MODE, key, inputFile, outputFile);
        }

        public static  void decrypt(String key, File inputFile, File outputFile)
                throws ExtendedException {
            doCrypto(Cipher.DECRYPT_MODE, key, inputFile, outputFile);
        }

        private static void doCrypto(int cipherMode, String key, File inputFile,
                File outputFile) throws ExtendedException {
            try {
                Key secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
                Cipher cipher = Cipher.getInstance(TRANSFORMATION);
                cipher.init(cipherMode, secretKey);

                FileInputStream inputStream = new FileInputStream(inputFile);


                CipherOutputStream out = new CipherOutputStream(new FileOutputStream(outputFile), cipher);
                byte[] buffer = new byte[8192];
                byte[] outputBytes = null;
                FileOutputStream outputStream = new FileOutputStream(outputFile);
                int count;
                while ((count = inputStream.read(buffer)) > 0)
                {
                    out.write(buffer, 0, count);
                    outputBytes = cipher.doFinal(buffer);

                }


                inputStream.close();
                outputStream.close();

            } catch (NoSuchPaddingException | NoSuchAlgorithmException
                    | InvalidKeyException | BadPaddingException
                    | IllegalBlockSizeException | IOException ex) {
                throw new ExtendedException("Error encrypting/decrypting file", ex);
            }
        }

1 个答案:

答案 0 :(得分:3)

只需使用CipherOutputStream即可。不要调用cipher.doFinal(buffer)并且不要忘记关闭输出流。

FileInputStream inputStream = new FileInputStream(inputFile);
FileOutputStream fileout = new FileOutputStream(outputFile);
CipherOutputStream out = new CipherOutputStream(fileout , cipher);

try {
    byte[] buffer = new byte[8192];
    int count;

    while ((count = inputStream.read(buffer)) > 0) {
        out.write(buffer, 0, count);
    }
} finally {
    out.close();
    inputStream.close();
}

CipherOutputStream为您管理密码。它将关闭流并调用内部缓冲区时调用doFinal