Tuesday 26 September 2017

Encrypt and Decrypt file/stream in Java

In this post, I am going to explain how to encrypt and decrypt the contents of a file using CipherOutputStream and CipherInputStream.

Encrypt data using CipherOutputStream
CipherOutputStream class is used to encrypt data before writing the data to underlying stream.

How to initialize CipherOutputStream?
CipherOutputStream class provides below public constructor to initialize.
public CipherOutputStream(OutputStream os, Cipher c)

What is Cipher?
Cipher is the primary class of java cryptography, is provides encryption and decryption functionality. Cipher class provides below static method to get a Cipher object.

public static final Cipher getInstance(String transformation) throws NoSuchAlgorithmException, NoSuchPaddingException

public static final Cipher getInstance(String transformation,String provider) throws NoSuchAlgorithmException, NoSuchProviderException,NoSuchPaddingException

public static final Cipher getInstance(String transformation, Provider provider) throws NoSuchAlgorithmException, NoSuchPaddingException

'transformation' specifies the operation to be performed on given input. Transformaton can be in one of below forms.
          a. "algorithm/mode/padding" or
          b. "algorithm"
         
Ex:
Ciper cipher = Cipher.getInstance("AES/CTR/PKCS5Padding");
Ciper cipher = Cipher.getInstance("AES");


What are the basic cipher transformations supported by Java?
Every implementation of the Java platform is required to support the following standard Cipher transformations.

Transformation
Key Size
AES/CBC/NoPadding
128
AES/CBC/PKCS5Padding
128
AES/ECB/NoPadding
128
AES/ECB/PKCS5Padding
128
DES/CBC/NoPadding
56
DES/CBC/PKCS5Padding
56
DES/ECB/NoPadding
56
DES/ECB/PKCS5Padding
56
DESede/CBC/NoPadding
168
DESede/CBC/PKCS5Padding
168
DESede/ECB/NoPadding
168
DESede/ECB/PKCS5Padding
168
RSA/ECB/PKCS1Padding
1024, 2048
RSA/ECB/OAEPWithSHA-1AndMGF1Padding
1024, 2048
RSA/ECB/OAEPWithSHA-256AndMGF1Padding
1024, 2048

Decrypt Data using CipherInputStream
CipherInputStream class provides a public constructor, that takes an input stream and cipher to decrypt the data. CipherInputStream reads the data from underlying input stream and use the cipher to decrypt the data.

How to initialize CipherInputStream object?
CipherInputStream class provides below public constructor to initialize CipherInputStream object.
CipherInputStream(InputStream is, Cipher c)

Below step-by-step procedure explain how to encrypt the data using CipherOutputStream and how to decrypt the data using CipherInputStream.

Step 1: Encrypt the data using CipherOutputStream.

          private static void encryptFile() throws Exception {
                    KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm);
                    keyGenerator.init(keySize);
                    key = keyGenerator.generateKey();
                    cipher = Cipher.getInstance(transformation);
                    cipher.init(Cipher.ENCRYPT_MODE, key);

                    try (OutputStream outputStream = new BufferedOutputStream(
                                        new CipherOutputStream(new FileOutputStream(file), cipher))) {
                              for (int i = 0; i < 10; i++) {
                                        outputStream.write(new String("Hello World\n").getBytes());
                              }

                    }

          }
         
Step 2: Decrypt the data using CipherInputStream.

          private static void decryptAndPrintFile() throws Exception {
                    Cipher cipherToDecrypt = Cipher.getInstance(transformation);
                    cipherToDecrypt.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(cipher.getIV()));

                    try (BufferedReader br = new BufferedReader(
                                        new InputStreamReader(new CipherInputStream(new FileInputStream(file), cipherToDecrypt)))) {

                              String line = null;

                              while ((line = br.readLine()) != null) {
                                        System.out.println(line);
                              }
                    }

          }
         

Find below working application.

CipherDemo.java
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.IvParameterSpec;

public class CipherDemo {

 /* File to write encrypted data */
 private static File file;
 private static String filePath = "a.txt";

 /* variables used to encrypt the data */
 private static int keySize = 128;
 private static String transformation = "AES/CBC/PKCS5Padding";
 private static String algorithm = "AES";
 private static Key key = null;
 private static Cipher cipher = null;

 /* Encrypt the data and write to the file a.txt. */
 private static void encryptFile() throws Exception {
  KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm);
  keyGenerator.init(keySize);
  key = keyGenerator.generateKey();
  cipher = Cipher.getInstance(transformation);
  cipher.init(Cipher.ENCRYPT_MODE, key);

  try (OutputStream outputStream = new BufferedOutputStream(
    new CipherOutputStream(new FileOutputStream(file), cipher))) {
   for (int i = 0; i < 10; i++) {
    outputStream.write(new String("Hello World\n").getBytes());
   }

  }

 }

 /* Print the contents of file a.txt */
 private static void printFile() throws IOException {
  try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)))) {
   String line = null;

   while ((line = br.readLine()) != null) {
    System.out.println(line);
   }
  }

 }

 /* Decrypt and print the contents of file a.txt. */
 private static void decryptAndPrintFile() throws Exception {
  Cipher cipherToDecrypt = Cipher.getInstance(transformation);
  cipherToDecrypt.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(cipher.getIV()));

  try (BufferedReader br = new BufferedReader(
    new InputStreamReader(new CipherInputStream(new FileInputStream(file), cipherToDecrypt)))) {

   String line = null;

   while ((line = br.readLine()) != null) {
    System.out.println(line);
   }
  }

 }

 public static void main(String args[]) throws Exception {

  file = new File(filePath);

  encryptFile();

  System.out.println("Data before Decrypting");
  System.out.println("***********************************");
  printFile();

  System.out.println("\nData After Decrypting");
  System.out.println("***********************************");
  decryptAndPrintFile();

 }
}

Output
Data before Decrypting
***********************************
 
ª¬ÆMÜ,×?oÎ?V:èL°*‡æO¸×âñ¤B øßN*#,l8)Ç#`¿
)—Ó 
²AòÏÏæã®tÖN[·Ž±†íVæ´ŒœÂú’VYI=å
µ¡šE­ÇîèŸÀe³Áó =?ö&t¬º$ë$»dlâ¼™“ Þðq°2

Data After Decrypting
***********************************
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World


No comments:

Post a Comment