Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Encrypt and Decrypt Files with Apache Commons IO

Header Image

Ahoy mateys! Welcome aboard on this journey of encryption and decryption using the powerful Apache Commons IO library. In today’s digital age, privacy and security are of utmost importance, especially when it comes to sensitive data. That’s why it’s crucial to know how to protect our files with encryption. But fret not, with the help of Apache Commons IO, it’s as easy as hoisting the Jolly Roger.

Encrypting and Decrypting Files Using the Library

The Apache Commons IO library provides several methods to encrypt and decrypt files using various algorithms such as AES, Blowfish, and DES. Here’s an example of how to encrypt a file using AES encryption:

File inputFile = new File("secret_document.txt");
File encryptedFile = new File("secret_document.enc");

String password = "avastye!"; // The password to encrypt the file

try (FileInputStream inputStream = new FileInputStream(inputFile);
     FileOutputStream outputStream = new FileOutputStream(encryptedFile)) {

    // Create a Cipher object with the AES encryption algorithm and initialize it with the password
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(password.getBytes(), "AES"));

    // Wrap the input stream with a CipherOutputStream to encrypt the data and write it to the output stream
    try (CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, cipher)) {
        IOUtils.copy(inputStream, cipherOutputStream);
    }

} catch (IOException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e) {
    System.err.println("Error encrypting file: " + e.getMessage());
}

In this code snippet, we first create a File object for the input file and another for the encrypted file. We then specify a password that will be used to encrypt the file. We create a Cipher object with the AES encryption algorithm and initialize it with the password. Next, we wrap the input stream with a CipherOutputStream to encrypt the data as it’s being read and write it to the output stream. Finally, we use the IOUtils.copy() method from the Apache Commons IO library to copy the encrypted data to the output file.

To decrypt the encrypted file, we can use the following code:

File encryptedFile = new File("secret_document.enc");
File decryptedFile = new File("secret_document_decrypted.txt");

String password = "avastye!"; // The password to decrypt the file

try (FileInputStream inputStream = new FileInputStream(encryptedFile);
     FileOutputStream outputStream = new FileOutputStream(decryptedFile)) {

    // Create a Cipher object with the AES encryption algorithm and initialize it with the password
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(password.getBytes(), "AES"));

    // Wrap the input stream with a CipherInputStream to decrypt the data and write it to the output stream
    try (CipherInputStream cipherInputStream = new CipherInputStream(inputStream, cipher)) {
        IOUtils.copy(cipherInputStream, outputStream);
    }

} catch (IOException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e) {
    System.err.println("Error decrypting file: " + e.getMessage());
}

This code is very similar to the previous code, but we create a Cipher object with the decryption mode and initialize it with the same password used for encryption. We wrap the input stream with a CipherInputStream to decrypt the data as it’s being read and write it to the output stream.

Conclusion

With the Apache Commons IO library, encrypting and decrypting files is as easy as navigating the treacherous seas witha trusty crew by your side. This library provides a wide range of algorithms for encryption and decryption, making it suitable for various use cases. In addition to encryption, the Apache Commons IO library provides several other useful functionalities like copying files, moving files, deleting files, and more.

Remember, encryption is an essential tool for protecting sensitive data, and Apache Commons IO makes it easy to implement. By following the examples and using the library’s features, you can ensure that your files are safe from prying eyes.

That’s it for today, me hearties! I hope you enjoyed this journey of encryption and decryption using Apache Commons IO. Keep learning and exploring, and may your code always run as smoothly as a ship on calm seas. Yo-ho-ho!