Pages Navigation Menu

Coding is much easier than you think

Encryption and Decryption of Data using AES-128 algorithm in Java

Encryption and Decryption of Data using AES-128 algorithm in Java

Encryption and Decryption of Data using AES-128 algorithm in Java

package com.simplecode.action;


import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.*;

public class AES 
private static String algorithm = "AES";
private static byte[] keyValue=new byte[] 
{ 'A', 'S', 'e', 'c', 'u', 'r', 'e', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y' };

        // Performs Encryption
        public static String encrypt(String plainText) throws Exception 
                Key key = generateKey();
                Cipher chiper = Cipher.getInstance(algorithm);
                chiper.init(Cipher.ENCRYPT_MODE, key);
                byte[] encVal = chiper.doFinal(plainText.getBytes());
                String encryptedValue = new BASE64Encoder().encode(encVal);
                return encryptedValue;

        // Performs decryption
        public static String decrypt(String encryptedText) throws Exception 
                // generate key 
                Key key = generateKey();
                Cipher chiper = Cipher.getInstance(algorithm);
                chiper.init(Cipher.DECRYPT_MODE, key);
                byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedText);
                byte[] decValue = chiper.doFinal(decordedValue);
                String decryptedValue = new String(decValue);
                return decryptedValue;

//generateKey() is used to generate a secret key for AES algorithm
        private static Key generateKey() throws Exception 
                Key key = new SecretKeySpec(keyValue, algorithm);
                return key;

        // performs encryption & decryption 
        public static void main(String[] args) throws Exception 

                String plainText = "Password";
                String encryptedText = AES.encrypt(plainText);
                String decryptedText = AES.decrypt(encryptedText);

                System.out.println("Plain Text : " + plainText);
                System.out.println("Encrypted Text : " + encryptedText);
                System.out.println("Decrypted Text : " + decryptedText);


About Mohaideen Jamil

Am currently working as a Struts 2, Webservices Developer in a reputed IT Organisations. I can help you with teaching Core java , Struts 2 and Webservices.

Cannot complete the install because one or more required items could not be found. Android Error
Microsoft SharePoint Server 2013 installation scenarios are not supported


  1. Thanks for your sharing but I would like to give some feedback whcih I read from stackoverflow.

    sun.misc.BASE64Decoder() is internal Sun code, is NOT part of J2SE (it, not portable), and may disappear at any time — Sun explicitly says to NOT use their internal libraries in user code

    • Hi,

      Yes I agree , using sun.misc.BASE64Decoder() was a bad practice of course, but I was using the above code for my 10 year old project :( :P, hence this code suits my requirement.

      If you have any better approach , please share it..
      Thanks :)

  2. How abt AES 192 logic??

  3. hai thank for your post…..but i need how to encrypt and decrypt List or ArrayList….

  4. I would like this!!!

  5. Thanks for sharing you save my lost of time. And that program really help me in my project.
    Thanks again

Leave a Reply Note To post source code in comment, use [code] [/code] tag

SimpleCodeStuffs located at , India, Chennai . Reviewed by rated: 8.8 / 10
%d bloggers like this: