I'd like to implement a simple encryption/decryption tool in java. Therefore I found a little tutorial on: http://www.codejava.net/coding/file-encryption-and-decryption-simple-example
I changed some lines of code, to guarantee the encryption of large files. Now I got the problem, that the decryption doen't work.
I got the following error message/exception:
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 I tried to change the Transoformation parameter to AES/CBC/PKCS5Padding but that has no effect. Does anyone know, how to optimize the given code?
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); } }