Tuesday, 26 September 2017

Java CipherInputStream example

In my previous post, I explained how to encrypt the content using CipherOutputStream. In this post, I am going to explain how to decrypt the encrypted content 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