Java annotations

 Do you want to add information about the code as meta data? If your answer is yes then this is the feature to add annotations.

Why is it needed?

It is used to provide supplementary information about the classes, member variables and functions.

What is the difference between annotations and comments?

Annotations provides the meta data about coding which can be used by tools and compilers. But comments are simple information to know about coding.

Syntax:

‘@’ annotation name                                                                                                                                       

Annotation types:

Annotations can be classified into the following types..

1. Built- in annotations:

These are annotations created by the java itself

Eg: ‘@Depreccated’, ’@Override’, ‘@SuppressWarnings’

2. Custom annotations:

The user can create this type of annotations.

Eg:

‘@interface’

Java code for built-in annotation:

These are included in java package itself. Here ‘@Override’ is used.

This annotation is used to tell the compiler that this base class code is overridden.

Base class or Super class: sClass1

Child class: cClass

Method: display() – same method with two definition is called overridden.  

Sample Code:

   class sClass1 {

       void display() {

           System.out.println("This is the Super Class");

       }

   }

   class cClass extends sClass1 {

       @Override

       void display() {

           System.out.println("This is the Child Class");

       }

   }

Next type of annotation is given below.

Java code for custom annotation:

              User defined annotations are called custom annotation. Here, the custom annotations are listed below..

@Retention :it specifies how long the annotations are retained.

@Target : It specifies the kind of program, the annotations are applicable.

@interface : user defined interface

@custAnnotation: user defined custom annotation name.

Sample Code:

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.METHOD)

public @interface custAnnotation {

    String name();

}

public class egClass {

    @custAnnotation("This is the custom annotation")

    public void egMethod() {

        // Method implementation

    }

}

These are the sample codes used to provide annotations in both built-in and custom type. Hope this codes are useful to you.Keep coding!!!

Symmetric Encryption in java

              Symmetric encryption is the process of using same key for both encryption and decryption.

Encryption: It is the process of converting plain text into ciphertext using an encryption key.

Decryption: It converts the ciphertext into original plain text using the same key.

Program implementation in java:

              Program implementation starts from reading input text from the user. A secret key is generated. The input is encrypted using the secret key. Same key is used for decryption.

Steps to follow:

  • Built-in packages javax.crypto.* ,java.util.Base64 and java.util.Scanner are included.
  • A public class is created with main() function.

Input reading:

  • Scanner object is created to read the system input.
  • Using this object, input text is read until the next line.

Secret key Generation:

  • KeyGenerator object is created to get instance for AES(Advanced Encryption Standard).
  • It is initialised as 128 bit.
  • generateKey() is used to generate secret key.

Encryption Process:

  • Cipher object is created and get assigned with instance of AES.
  • Cipher object is initialised with encrypted mode and secret key.
  • A byte of data is created from input text.
  • It gets encoded Base64.getEncoder(). The encrypted text is printed in the output screen.

Decryption Process:

  • Cipher object is initialised with Decrypted mode and secret key.
  • The data is decoded using Base64 decoder.
  • Finally, it displays in the output screen.          

Program:

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

import javax.crypto.spec.SecretKeySpec;

import java.util.Base64;

import java.util.Scanner;

public class SymmetricEncryptionEg {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        // Get the input from the user

        System.out.println("Enter a string: ");

        String inputTxt = scanner.nextLine();  

        try {

            // A secret key is generated

            KeyGenerator keyGenerator1 = KeyGenerator.getInstance("AES");

            keyGenerator1.init(128);

            SecretKey secrKey = keyGenerator1.generateKey();

            // Let us Encrypt the input text

            Cipher cipher1 = Cipher.getInstance("AES");

            cipher1.init(Cipher.ENCRYPT_MODE, secrKey);

            byte[] encryptBytes = cipher1.doFinal(inputTxt.getBytes());

            String encryptTxt = Base64.getEncoder().encodeToString(encryptBytes);

            System.out.println("The Encrypted Text is: " + encryptTxt);

            // it is the process of Decrypting the encrypted text

            cipher1.init(Cipher.DECRYPT_MODE, secrKey);

            byte[] decryptBytes = cipher1.doFinal(Base64.getDecoder().decode(encryptTxt));

            String decryptTxt = new String(decryptBytes);

            System.out.println("The Decrypted Text is : " + decryptTxt);

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}

Output:

C:\raji\blog>javac SymmetricEncryptionEg.java

C:\raji\blog>java SymmetricEncryptionEg

Enter a string:

Welcome to Cryptography in java

The Encrypted Text is: TK8+C2B1+DwJ+CyFPc2YhOn1uRRN4V9W6n1KAGgtNd0=

The Decrypted Text is : Welcome to Cryptography in java

This is the way of implementing Symmetric encryption in java. Keep coding!!!