Sunday, August 11, 2013

Java - Advanced Encryption Standard (AES) Algorithm

 
AESAlgorithm.java
 
package com.java.aes;

import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
import com.sun.org.apache.xml.internal.security.utils.Base64;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

/**
 *
 * @author dhanoopbhaskar
 */
public class AESAlgorithm {

    KeyGenerator keyGenerator = null;
    SecretKey secretKey = null;
    Cipher cipher = null;

    public AESAlgorithm() {
        try {
            /**
             * Create a AES key
             */
            keyGenerator = KeyGenerator.getInstance("AES");
            secretKey = keyGenerator.generateKey();

            /**
             * Create an instance of cipher providing the following info
             * separated by slash.
             *
             *      - Algorithm name
             *      - Mode (optional)
             *      - Padding scheme (optional)
             *
             * NB:
             *      AES = Advanced Encryption Standard.
             *      ECB = Electronic Codebook mode.
             *      PKCS5Padding = PKCS #5-style padding.
             */
            cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        } catch (NoSuchPaddingException ex) {
            System.out.println(ex);
        } catch (NoSuchAlgorithmException ex) {
            System.out.println(ex);
        }

    }

    /**
     *
     * @param plainText
     * @return cipherBytes
     */
    public byte[] encryptText(String plainText) {
        byte[] cipherBytes = null;
        try {
            /**
             * Initialize the cipher for encryption
             */
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            /**
             * Convert the text string to byte format
             */
            byte[] plainBytes = plainText.getBytes();
            /**
             * Perform encryption with method doFinal()
             */
            cipherBytes = cipher.doFinal(plainBytes);
        } catch (IllegalBlockSizeException ex) {
            System.out.println(ex);
        } catch (BadPaddingException ex) {
            System.out.println(ex);
        } catch (InvalidKeyException ex) {
            System.out.println(ex);
        }

        return cipherBytes;
    }

    /**
     *
     * @param cipherBytes
     * @return plainText
     */
    public String decryptText(byte[] cipherBytes) {
        String plainText = null;
        try {
            /**
             * Initialize the cipher for decryption
             */
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            /**
             * Perform decryption with method doFinal()
             */
            byte[] plainBytes = cipher.doFinal(cipherBytes);
            /**
             * Convert encrypted text to string format
             */
            plainText = new String(plainBytes);
        } catch (IllegalBlockSizeException ex) {
            System.out.println(ex);
        } catch (BadPaddingException ex) {
            System.out.println(ex);
        } catch (InvalidKeyException ex) {
            System.out.println(ex);
        }

        return plainText;
    }

    /**
     *
     * @param plainText
     * @return cipherText
     */
    public String encrypt(String plainText) {
        String cipherText = null;
        byte[] cipherBytes = encryptText(plainText);
        cipherText = bytesToString(cipherBytes);
        return cipherText;
    }

    /**
     * 
     * @param cipherText
     * @return plainText
     */
    public String decrypt(String cipherText) {
        String plainText = null;
        byte[] cipherBytes = stringToBytes(cipherText);
        plainText = decryptText(cipherBytes);
        return plainText;
    }

    public static void main(String[] args) {
        AESAlgorithm desAlgorithm = new AESAlgorithm();
        String textToEncrypt = "AES Algorithm";
        System.out.println("Text before Encryption: " + textToEncrypt);
        String cipherText = desAlgorithm.encrypt(textToEncrypt);
        System.out.println("Cipher Text: " + cipherText);
        System.out.println("Text after Decryption: " + desAlgorithm.decrypt(cipherText));
    }

    /**
     * 
     * @param rawText
     * @return plainText
     *
     * Perform Base64 encoding
     */
    private String bytesToString(byte[] rawText) {
        String plainText = null;
        plainText = Base64.encode(rawText);
        return plainText;
    }

    /**
     * 
     * @param plainText
     * @return rawText
     *
     * Perform Base64 decoding
     */
    private byte[] stringToBytes(String plainText) {
        byte[] rawText = null;
        try {
            rawText = Base64.decode(plainText);
        } catch (Base64DecodingException ex) {
            System.out.println(ex);
        }
        return rawText;
    }
}

Output: 
Text before Encryption: AES Algorithm 
Cipher Text: rqDMYCW17enP8S7sio3Kbg== 
Text after Decryption: AES Algorithm

0 comments:

Post a Comment

Contact Form

Name

Email *

Message *