Encrypt And Decrypt A File In Java
Posted By : S. Ajit | 28-May-2017
In this blog we will encrypt a text file and decrypt the same text file using Advanced Encryption Standard(AES) . If we are using AES then the secret key or password that we are going to use for encryption or decryption should be of length 16,24,32 . In java javax.crypto package provide classes and interfaces for cryptographic operations.
Now, let’s see some real example,
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.Key;
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 Crypto {
static void fileProcessor(int cipherMode,String key,File inputFile,File outputFile){
try {
Key secretKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(cipherMode, secretKey);
FileInputStream inputStream = new FileInputStream(inputFile);
byte[] inputBytes = new byte[(int) inputFile.length()];
inputStream.read(inputBytes);
byte[] outputBytes = cipher.doFinal(inputBytes);
FileOutputStream outputStream = new FileOutputStream(outputFile);
outputStream.write(outputBytes);
inputStream.close();
outputStream.close();
} catch (NoSuchPaddingException | NoSuchAlgorithmException
| InvalidKeyException | BadPaddingException
| IllegalBlockSizeException | IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String key = "This is a secret";
File inputFile = new File("text.txt");
File encryptedFile = new File("text.encrypted");
File decryptedFile = new File("decrypted-text.txt");
try {
Crypto.fileProcessor(Cipher.ENCRYPT_MODE,key,inputFile,encryptedFile);
Crypto.fileProcessor(Cipher.DECRYPT_MODE,key,encryptedFile,decryptedFile);
System.out.println("Sucess");
} catch (Exception ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
}
}
}
The above program takes text.txt file as a input and generate two new file text.encrypted and decrypted-text.txt. To execute this program you will need to create a text.txt file and put some text in it. text.txt will be encrypted to text.encrypted file using AES and secret key. We will use this encrypted file to decrypt it using the same secret key.
We have used Cipher class which provides the functionality of a cryptographic cipher for encryption and decryption.
The doFinal method of Cipher class finishes a multiple-part encryption or decryption operation. init method of initialize cipher with a key and cipher mode . To encrypt a file we have used ENCRYPT_MODE cipher mode for encryption and for decryption DECRYPT_MODE.
Thanks
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
S. Ajit
Ajit is a software developer who loves solving problems and programming in C, C++, Java. He also has a passion to learn new technologies.