Program to Encrypt and decrypt text using password - Write encrypted text to file | Read encrypted text from file in java


We can use jasypt (java simplified encryption) library for encrypting and decrypting text of file.

Download : jasypt jars from here


In this post we’ll write following programs >
  • Program 1 to Write encrypted text to file in java
  • Program 2 to Read encrypted text from file and decrypt in java



Program 1 to Write encrypted text to file in java
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.jasypt.util.text.BasicTextEncryptor;
/** JavaMadeSoEasy.com */
public class WriteEncryptedTextToFile {
   public static void main(String... args) {
          FileOutputStream fos = null;
          BufferedOutputStream bos = null;
          String fileName= "c:/myFile.txt";
          try {
                 fos = new FileOutputStream(fileName);
                 bos = new BufferedOutputStream(fos);
                 String originalText= "You are learning File IO from javaMadeSoEasy.com";
                 System.out.println("originalText = "+ originalText);
                
                 /*
                 * Encrypting a text
                 */
                 BasicTextEncryptor basicTextEncryptor = new BasicTextEncryptor();
                 //Must remember this password, as it will be required at time of decryption
                 basicTextEncryptor.setPassword("password");
                 String encryptedText = basicTextEncryptor.encrypt(originalText);
                 System.out.println("encryptedText = "+ encryptedText);
                 //convert encryptedText into byte array to write it in file
                 bos.write(encryptedText.getBytes());               
                 bos.flush();
                
                 System.out.println("encryptedText has been written successfully in "
                              +fileName);
          } catch (IOException e) {
                 e.printStackTrace();
          } finally {
                 try {
                       if (bos != null){
                              bos.close(); //close FileOutputStream
                       }
                       if (fos != null){
                              fos.close(); //close FileOutputStream
                       }
                
                 } catch (IOException e) {
                       e.printStackTrace();
                 }
          }
   }
}
/* OUTPUT
originalText = You are learning File IO from javaMadeSoEasy.com
encryptedText = nkVKfkQhiTj1WgNX6GuyeCW14XVrpf7lJs0X0mOdOeVA2Wy3vWUNuGpfdkcLc8ZsfNS/6S3Hnr3Wu5yAohq2ng==
encryptedText has been written successfully in c:/myFile.txt
*/


After executing above program c:/myFile.txt will look like this >
nkVKfkQhiTj1WgNX6GuyeCW14XVrpf7lJs0X0mOdOeVA2Wy3vWUNuGpfdkcLc8ZsfNS/6S3Hnr3Wu5yAohq2ng==




Now, we will write program to read encrypted test (i.e. read encrypted text and decrypt it)


Program 2 to Read encrypted text from file in java

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import org.jasypt.util.text.BasicTextEncryptor;
/** JavaMadeSoEasy.com */
public class ReadEncryptedTextFromFile {
   public static void main(String... args) {
          FileInputStream fis = null;
          BufferedInputStream bis = null;
         
          try {
                 fis = new FileInputStream("c:/myFile.txt");
                 bis= new BufferedInputStream(fis);
                
                
                 //create byteArray of file's size
                 byte[] bytes = new byte[fis.available()];
                 fis.read(bytes); //Read file in ByteArray
                
                 String encryptedTextOfFile=new String(bytes); //convert ByteArray to String
                 System.out.println("encryptedTextOfFile = "+encryptedTextOfFile);
                
                 /*
                 * Decrypting text of file
                 */
                 BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
                 //Give password given at time of encryption
                 textEncryptor.setPassword("password");
                
                 String decryptedTextOfFile= textEncryptor.decrypt(encryptedTextOfFile);
             System.out.println("decryptedTextOfFile = "+decryptedTextOfFile);
          } catch (IOException e) {
                 e.printStackTrace();
          } finally {
                 try {
                       if (fis != null)
                              fis.close(); //close FileInputStream
                       if (bis != null)
                              bis.close(); //close BufferedInputStream
                 } catch (IOException e) {
                       e.printStackTrace();
                 }
          }
   }
}
/*OUTPUT
encryptedTextOfFile = nkVKfkQhiTj1WgNX6GuyeCW14XVrpf7lJs0X0mOdOeVA2Wy3vWUNuGpfdkcLc8ZsfNS/6S3Hnr3Wu5yAohq2ng==
decryptedTextOfFile = You are learning File IO from javaMadeSoEasy.com
*/

If wrong password is given at time of decryption than org.jasypt.exceptions.EncryptionOperationNotPossibleException is thrown.

RELATED LINKS>

Difference between FileInputStream and BufferedInputStream in java file IO

Read text from file using FileInputStream in java file IO


Program to Read text from file using FileInputStream and Try with resource provided in java 7


No comments:

Post a Comment