Working With Cipher Class in Java

In this article, we are going to describe crypto graphic data security by using cipher class of java. I describe a Cipher class with its constructor or method details.

Introduction

 
In this article, we are going to describe cryptographic data security using the cipher class of Java. I describe a Cipher class with its constructor or method details. And I give you an example; that's why you can easily understand its uses.
 

Encryption and Decryption

 
Encryption is the conversion of data into a form, called a cipher text, that cannot be easily understood by unauthorized people. Decryption is the process of converting encrypted data back into its original form, so it can be understood. In other words, cryptography and encryption is the process of transforming information (referred to as plaintext) using an algorithm (called a cipher) to make it unreadable to anyone except those possessing a special knowledge, usually referred to as a key. The result of the process is encrypted information (in cryptography, referred to as cipher text). The reverse process, i.e., to make the encrypted information readable again, is referred to as decryption.
 
               Cipher Class in Java 
 

Cipher class

 
In Java Cipher is a sprat class and this class is given in the javax.crypto package. This class is specially designed for encryption and decryption. It provides the functionality of a cryptographic cipher for encryption and decryption. It forms the core of the Java Cryptographic Extension (JCE) framework. In order to create a Cipher object, the application calls the Cipher's getInstance method, and passes the name of the requested transformation to it. Optionally, the name of a provider may be specified.
 

Cipher Constructor details

 
This class has only one constructor and the constructor takes three arguments.
 
Syntax
 
  1. protected Cipher(CipherSpi cipherSpi ,Provider provider,String transformation)  
Argument summary
 
cipherSpi - the delegate
provider - the provider
transformation - the transformation
 
Example
  1. import javax.crypto.Cipher;  
  2. import javax.crypto.KeyGenerator;  
  3. import javax.crypto.SecretKey;  
  4. import javax.swing.JOptionPane;  
  5. public class EncryptionExample {  
  6.       public static void main(String[] args) throws Exception {  
  7.             // create a key generator based upon the Blowfish cipher  
  8.             KeyGenerator keygenerator = KeyGenerator.getInstance("Blowfish");  
  9.             // create a key  
  10.             SecretKey secretkey = keygenerator.generateKey();  
  11.             // create a cipher based upon Blowfish  
  12.             Cipher cipher = Cipher.getInstance("Blowfish");  
  13.             // initialise cipher to with secret key  
  14.             cipher.init(Cipher.ENCRYPT_MODE, secretkey);  
  15.             // get the text to encrypt  
  16.             String inputText = JOptionPane.showInputDialog("Input your message: ");  
  17.             // encrypt message  
  18.             byte[] encrypted = cipher.doFinal(inputText.getBytes());  
  19.             // re-initialise the cipher to be in decrypt mode  
  20.             cipher.init(Cipher.DECRYPT_MODE, secretkey);  
  21.             // decrypt message  
  22.             byte[] decrypted = cipher.doFinal(encrypted);  
  23.             // and display the results  
  24.             JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),  
  25.                         "Enter text for Encryption: " + new String(encrypted) + "\n"  
  26.                                     + "Decryted tag: " + new String(decrypted));  
  27.             // end example  
  28.             System.exit(0);  
  29.       }  
  30. }  
OUTPUT
 
cmd output
 
encryption1.jpg
 
The following dialog box will appear
 
encryption in java
 
Now you enter your message in the following dialog box
 
encryption in java
 
The following dialog box show you the message in the encrypted form and decrypted form.
 
encryption in java