Overview
In this tutorial, we show you how to encrypt and decrypt a file using AES in Java 10 Example. We’ll encrypt the text.txt file using your secret key with the Advanced Encryption Standard (AES) as the symmetric encryption algorithm. Then we decrypt this file using the same secret key.Reference Tutorials
Create a text.txt file in a specific folder
We will create the text.txt file in the D drive.D:\\text.txt
This is a file used for testing encryption and decryption
Encryption
First, we'll create an instance of the Cipher class using the encryption algorithm called AES.var cipher = Cipher.getInstance("AES");Next, we will initialize a Cipher by calling its init() method with Cipher.ENCRYPT_MODE and your secret key as parameters:
cipher.init(Cipher.ENCRYPT_MODE, key);Then, we'll read the contents from the text.txt file into the array.
var fileInput = new File(fileInputPath); var inputStream = new FileInputStream(fileInput); var inputBytes = new byte[(int) fileInput.length()]; inputStream.read(inputBytes);After that, we'll call the doFinal() with the data to encrypt
var outputBytes = cipher.doFinal(inputBytes);Finally, we use FileOutputStream write the outputBytes and close.
var fileEncryptOut = new File(fileOutPath); var outputStream = new FileOutputStream(fileEncryptOut); outputStream.write(outputBytes); inputStream.close(); outputStream.close();Complete Code Example
public static void encryptedFile(String secretKey, String fileInputPath, String fileOutPath) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException, IllegalBlockSizeException, BadPaddingException { var key = new SecretKeySpec(secretKey.getBytes(), "AES"); var cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, key); var fileInput = new File(fileInputPath); var inputStream = new FileInputStream(fileInput); var inputBytes = new byte[(int) fileInput.length()]; inputStream.read(inputBytes); var outputBytes = cipher.doFinal(inputBytes); var fileEncryptOut = new File(fileOutPath); var outputStream = new FileOutputStream(fileEncryptOut); outputStream.write(outputBytes); inputStream.close(); outputStream.close(); System.out.println("File successfully encrypted!"); System.out.println("New File: " + fileOutPath); }
Decryption
Same as in the example above. First, we'll create an instance of the Cipher class using the encryption algorithm called AES.
var cipher = Cipher.getInstance("AES");Next, we will initialize a Cipher by calling its init() method with Cipher.DECRYPT_MODE and your secret key as parameters:
cipher.init(Cipher.ENCRYPT_MODE, key);Then, We'll read the contents from the text.txt file into the array.
var fileInput = new File(fileInputPath); var inputStream = new FileInputStream(fileInput); var inputBytes = new byte[(int) fileInput.length()]; inputStream.read(inputBytes);After that, We'll call the doFinal() with the data to encrypt
var outputBytes = cipher.doFinal(inputBytes);Finally, we use FileOutputStream write the outputBytes and close.
var fileEncryptOut = new File(fileOutPath); var outputStream = new FileOutputStream(fileEncryptOut); outputStream.write(outputBytes); inputStream.close(); outputStream.close();Complete Code Example
public static void decryptedFile(String secretKey, String fileInputPath, String fileOutPath) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException, IllegalBlockSizeException, BadPaddingException { var key = new SecretKeySpec(secretKey.getBytes(), "AES"); var cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, key); var fileInput = new File(fileInputPath); var inputStream = new FileInputStream(fileInput); var inputBytes = new byte[(int) fileInput.length()]; inputStream.read(inputBytes); byte[] outputBytes = cipher.doFinal(inputBytes); var fileEncryptOut = new File(fileOutPath); var outputStream = new FileOutputStream(fileEncryptOut); outputStream.write(outputBytes); inputStream.close(); outputStream.close(); System.out.println("File successfully decrypted!"); System.out.println("New File: " + fileOutPath); }
Here is the code for encrypt and decrypt a file using AES
package com.jackrutorial; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.spec.SecretKeySpec; public class EncryptDecryptFileExample { public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, IOException { var key = "jackrutorial.com"; System.out.println("File input: " + "D:\\text.txt"); //encryptedFile encryptedFile(key, "D:\\text.txt", "D:\\text.enc"); //decryptedFile decryptedFile(key, "D:\\text.enc", "D:\\text-decrypt.txt"); } public static void encryptedFile(String secretKey, String fileInputPath, String fileOutPath) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException, IllegalBlockSizeException, BadPaddingException { var key = new SecretKeySpec(secretKey.getBytes(), "AES"); var cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, key); var fileInput = new File(fileInputPath); var inputStream = new FileInputStream(fileInput); var inputBytes = new byte[(int) fileInput.length()]; inputStream.read(inputBytes); var outputBytes = cipher.doFinal(inputBytes); var fileEncryptOut = new File(fileOutPath); var outputStream = new FileOutputStream(fileEncryptOut); outputStream.write(outputBytes); inputStream.close(); outputStream.close(); System.out.println("File successfully encrypted!"); System.out.println("New File: " + fileOutPath); } public static void decryptedFile(String secretKey, String fileInputPath, String fileOutPath) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException, IllegalBlockSizeException, BadPaddingException { var key = new SecretKeySpec(secretKey.getBytes(), "AES"); var cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, key); var fileInput = new File(fileInputPath); var inputStream = new FileInputStream(fileInput); var inputBytes = new byte[(int) fileInput.length()]; inputStream.read(inputBytes); byte[] outputBytes = cipher.doFinal(inputBytes); var fileEncryptOut = new File(fileOutPath); var outputStream = new FileOutputStream(fileEncryptOut); outputStream.write(outputBytes); inputStream.close(); outputStream.close(); System.out.println("File successfully decrypted!"); System.out.println("New File: " + fileOutPath); } }
Output
text.txt filetext.enc
text-decrypt.txt