Tuesday 26 September 2017

Java CipherOutputStream example

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, it 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


Below step-by-step procedure explains how to encrypt the data using CipherOutputStream.

Step 1: Define Cipher instance.
                    KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm);
                    keyGenerator.init(keySize);
                    Key key = keyGenerator.generateKey();
                    Cipher cipher = Cipher.getInstance(transformation);
                    cipher.init(Cipher.ENCRYPT_MODE, key);

Step 2: Define OutputStream using CipherOutputStream.

outputStream outputStream = new BufferedOutputStream(new CipherOutputStream(new FileOutputStream(file), cipher));

Step 3: Use the output stream defined in step 2, to write some data to file.

          for (int i = 0; i < 10; i++) {
                    outputStream.write(new String("Hello World\n").getBytes());
          }

Find the below working application.


CipherOutputStreamDemo.java
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;

public class CipherOutputStreamDemo {

 /* 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;

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

  file = new File(filePath);

  /* Define cipher */
  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());
   }

  }

 }
}


No comments:

Post a Comment