Thursday, March 15, 2012

Implementation of Java Cryptography Extension

Implementation of Java Cryptography Extension (JCE) in Programs

Dear friend, 
JCE is a huge topic in Java programming language. There are many books written on the same.
I am writing a basic example to show how it works, for complete details and loop holes please 
google it:

I will show: how do use JCE to encrypt or decrypt a text in Data Encryption Standard (DES) mechanism.

Steps to do:
1) Generate the key.
2) Create the Cipher.
3) Initialize the Cipher for Encryption using key.
4) Encrypt the text.
5) Again initialize the Cipher for Decryption using the same key.
6) Decrypt the text.


import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

public class CryptoMain {    
    public static void main(String[] argv) {
        try {
            //Generate the Key
            KeyGenerator keygenerator = KeyGenerator.getInstance("DES");            
            SecretKey myDesKey = keygenerator.generateKey();

            //Create the cipher
            Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); 

            // Initialize the cipher for encryption
            desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);

            //sensitive information
            byte[] text = "Password_Content".getBytes();

            System.out.println("Text [Byte Format] : " + text);
            System.out.println("Text : " + new String(text));

            //Encrypt the text
            byte[] textEncrypted = desCipher.doFinal(text);

            System.out.println("Text Encrypted : " + textEncrypted);

            //Initialize the same cipher for decryption, using the same key
            desCipher.init(Cipher.DECRYPT_MODE, myDesKey);

            //Decrypt the text
            byte[] textDecrypted = desCipher.doFinal(textEncrypted);
            System.out.println("Text Decrypted : " + new String(textDecrypted));
        catch(NoSuchAlgorithmException e){
        catch(NoSuchPaddingException e){
        catch(InvalidKeyException e){
        catch(IllegalBlockSizeException e){
        catch(BadPaddingException e){

Text [Byte Format] : [B@1f585b
Text : Password_Content
Text Encrypted : [B@1f593c
Text Decrypted : Password_Content

Details about creation of cipher: Create a Cipher instance from Cipher class, specify the following 
information and separated by a slashes (/).
    a. Algorithm name
    b. Mode (optional)
    c. Padding scheme (optional)

    Cipher desCipher;
    // Create the cipher 
    desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

Note : 
    DES = Data Encryption Standard. 
    ECB = Electronic Codebook mode.
    PKCS5Padding = PKCS #5-style padding.
In this case, you created a DES (Data Encryption Standard) cipher in Electronic Codebook mode, with 
PKCS #5-style padding.


No comments:

Post a Comment