Thursday, 28 September 2017

ABAP: CONTINUE: Skip the iteration of loop

CONTINUE statement skips the current iteration of loop (DO–ENDDO, WHILE-ENDWHILE)

Syntax
CONTINUE.

Z_HELLO_WORLD

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

DATA counter TYPE I VALUE -1.

* Print only Even Numbers
Write 'Even counters from 0 to 10 are'.

WHILE counter < 10.
  counter = counter + 1.
  IF counter MOD 2 <> 0.
    CONTINUE.
  ENDIF.

  Write: / 'Value of counter : ', counter.

ENDWHILE.



Previous                                                 Next                                                 Home

ABAP: WHILE-ENDWHILE loop

It is also called as conditional loop. It execute the statements until the condition is true.

Syntax
WHILE condition
         * Execute block of statements.
ENDWHILE.

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

DATA counter TYPE I VALUE 0.

WHILE counter < 10.
  Write: / 'Value of counter : ', counter.
  counter = counter + 1.
ENDWHILE.



Previous                                                 Next                                                 Home

Wednesday, 27 September 2017

ABAP: DO-ENDDO loop

'DO-ENDDO' is also called as unconditional loop, it is used to execute block of statements given number of times.

Syntax
DO N times.
         * Execute this block
ENDDO.

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

DATA n TYPE I VALUE 10.
DATA counter TYPE I VALUE 0.

DO n TIMES.
  Write: / 'Value of counter : ', counter.
  counter = counter + 1.
ENDDO.



Previous                                                 Next                                                 Home

ABAP: Loops

Loops are used to execute a block of statements more than once. ABAP provides below statements to loop the statements.
         a. DO –ENDDO (Unconditional loop)

         b. WHILE - ENDWHILE (Conditional loop)


Previous                                                 Next                                                 Home

ABAP: CASE-ENDCASE statement

CASE-ENDCASE statement is used to check multiple conditions.

Syntax
CASE variable.
         WHEN val1.
                 * Execute statments if variable is equal to val1
         WHEN val2.
                 * Execute statements if variable is equal to val2
         WHEN OTHERS.
                 * Execute this block, if variable is not match to any of the values above.
ENDCASE.

Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*& Report Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT Z_HELLO_WORLD.
DATA day_of_the_week TYPE I VALUE 3.

CASE day_of_the_week.
  WHEN 1.
    Write 'Monday'.
  WHEN 2.
    Write 'Tuesday'.
  WHEN 3.
    Write 'Wednesday'.
  WHEN 4.
    Write 'Thrusday'.
  WHEN 5.
    Write 'Friday'.
  WHEN 6.
    Write 'Saturday'.
  WHEN 7.
    Write 'Sunday'.
  WHEN OTHERS.
    Write 'Invalid Day'.
ENDCASE.



Previous                                                 Next                                                 Home

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


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