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());
   }

  }

 }
}


ABAP: IF-ELSEIF statement

IF-ELSEIF statement is used to check multiple conditions.

Syntax
IF conditon1.
         * Execute this block,if condition1 is true
ELSEIF condition2.
         * Execute this block, if condition2 is true
ELSEIF condition3.
         * Execute this block, if condition3 is true
ELSE.
         * Execute this block, if all the conditions are false
ENDIF


Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*& Report Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT Z_HELLO_WORLD.

DATA marks TYPE I VALUE 55.

IF marks < 35.
  Write 'Failed'.
ELSEIF marks < 60.
  Write 'Second Class'.
ELSEIF marks < 70.
  Write 'First Class'.
ELSE.
  Write 'Destinction'.
ENDIF.



Previous                                                 Next                                                 Home

Friday, 22 September 2017

ABAP: IF-ELSE statement

IF-ELSE statement is an enhancement to IF statement that I explained in my previous post. The statements preceded by IF statement will execute only the condition is true, else the statements preceded by ELSE statement will execute.

Syntax
IF condition.
         * statements
ELSE.
         * statements
ENDIF.

Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*& Report Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT Z_HELLO_WORLD.

DATA var1 TYPE I VALUE 10.
DATA var2 TYPE I VALUE 20.

IF var1 > 10.
  Write 'var1 is > 10'.
ELSE.
  Write / 'var1 is <= 10'.
ENDIF.

IF var2 > 10.
  Write / 'var2 is > 10'.
ELSE.
  Write / 'var2 is <= 10'.
ENDIF.



Previous                                                 Next                                                 Home

Thursday, 21 September 2017

ABAP: IF statement

IF statement is used to execute block of statements only the given condition is true.

Syntax
IF condition.
         * Block of statements
ENDIF.

IF statement must terminated by ENDIF statement. The statements between IF and ENDIF are executed only the condition is true.

Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*& Report Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT Z_HELLO_WORLD.

DATA var1 TYPE I VALUE 10.
DATA var2 TYPE I VALUE 20.
DATA var3 TYPE I VALUE 30.

IF var1 > 10.
  Write 'var1 is > 10'.
ENDIF.

IF var2 > 10.
  Write / 'var2 is > 10'.
ENDIF.

IF var3 > 10.
  Write / 'var3 is > 10'.
ENDIF.




Previous                                                 Next                                                 Home

ABAP: Conditional statements

ABAP provides following conditional statements.
a.   IF statement
b.   IF-ELSE statement
c.   IF-ELSEIF-ELSE statement

d.   CASE-ENDCASE statement



Previous                                                 Next                                                 Home

ABAP: Clear Variables

By using CLEAR keyword, we can clear the values assigned to a variable.

Syntax
CLEAR: var1, var2...varN

Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*& Report Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT Z_HELLO_WORLD.

DATA var1 TYPE I VALUE 10.
DATA var2 TYPE I VALUE 20.
DATA var3 TYPE I VALUE 30.

Write 'Before Clearing : '.
Write: var1, var2, var3.

CLEAR: var1,var2, var3.

Write / 'After Clearing ; '.
Write: var1, var2, var3.



Previous                                                 Next                                                 Home

ABAP: MOVE: Assign value to a variable

Just like how we assign value to a variable using '=' operator, we cal also assign value to a variable using MOVE keyword.

Syntax
MOVE value TO variable.

Ex:
MOVE 10 TO var1.

Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*& Report Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT Z_HELLO_WORLD.

DATA var1 TYPE I.

MOVE 10 TO var1.
Write 'Value of var1 is : '.
Write var1.

MOVE -450 TO var1.
Write / 'Value of var1 is : '.
Write var1.


Previous                                                 Next                                                 Home