AESUtility.java

package com.americanexpress.gpzn.services.processservice.galaxy.v1.util;

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;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;

public class AESEncryptionDecryption {

 private static final String CLASS_NAME = "AESEncryptionDecryption";
 // 16 bytes or 128 bit Cipher Key..
 private static final String CIPHER_KEY = "054cbdad4808e3de";
 private static final String AES_ENCRYPTION = "AES";
 private static byte[] keyBytes = CIPHER_KEY.getBytes();

 public static void main(String args[]) {

 // Encrypting the Given String DevelopProjects.com using AES algorithm
 String encryptedString = getEncryptedString("DevelopProjects.com");
 System.out.println("Encrypted String generated using the AES :::"
 + encryptedString);
 // Decrypting the Encrypted String using AES algorithm
 String decryptedString = getDecryptStatus(encryptedString);
 System.out.println("Decrypted String generated using the AES :::"
 + decryptedString);
 }

 /*
 * This is the encryption method where we will be the using the Cipher key
 * to encrypt using AES Algorithm and encode using Apache Byte Code . We
 * will pass the input String appended with time stamp to Cipher to get an
 * encrypted String and we will it encode it using Apache byte.
 */
 public static String getEncryptedString(String inputString) {
 String methodName = "getEncryptedString";
 System.out.println(methodName + "***inputString::" + inputString);
 String encodedString = "";
 byte[] messageBytes = inputString.getBytes();
 SecretKeySpec secretKeySpecification = new SecretKeySpec(keyBytes,
 AES_ENCRYPTION);
 Cipher cipher;
 try {
 // Code to Encrypt
 cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
 cipher.init(Cipher.ENCRYPT_MODE, secretKeySpecification);
 byte[] encryptedBytes = cipher.doFinal(messageBytes);
 encodedString = Base64.encodeBase64URLSafeString(encryptedBytes);
 } catch (NoSuchAlgorithmException e) {
 System.out.println(methodName + " Exception::" + e.getMessage());

 } catch (NoSuchPaddingException e) {
 System.out.println(methodName + " Exception::" + e.getMessage());
 } catch (InvalidKeyException e) {
 System.out.println(methodName + " Exception::" + e.getMessage());
 } catch (IllegalBlockSizeException e) {
 System.out.println("Exception::" + e.getMessage());
 } catch (BadPaddingException e) {
 System.out.println(methodName + " Exception::" + e.getMessage());
 }
 return encodedString;
 }

 /*
 * This is the decryption method where we will be the using the Cipher key
 * to decrypt using AES Algorithm and decode using Apache Byte Code . We
 * will be decoding the encoded String which we will get from WEBUI and
 * decrypt the decoded String
 */
 public static String getDecryptStatus(String encryptedString) {
 String methodName = "getDecryptStatus";

 System.out.println(methodName
 + " Entry. Given encryptedString input is " + encryptedString);

 String decryptedString = "";
 SecretKeySpec secretKeySpecification = new SecretKeySpec(keyBytes,
 AES_ENCRYPTION);
 Cipher cipher;
 try {
 // Code to Decrypt
 cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
 cipher.init(Cipher.DECRYPT_MODE, secretKeySpecification);
 byte[] decodedBytes = Base64.decodeBase64(encryptedString);

 byte[] originalMessage = cipher.doFinal(decodedBytes);

 decryptedString = new String(originalMessage);

 } catch (NoSuchAlgorithmException e) {
 System.out.println(methodName + " Exception::" + e.getMessage());
 } catch (NoSuchPaddingException e) {
 System.out.println(methodName + " Exception::" + e.getMessage());
 } catch (InvalidKeyException e) {
 System.out.println(methodName + " Exception::" + e.getMessage());
 } catch (IllegalBlockSizeException e) {
 System.out.println(methodName + " Exception::" + e.getMessage());
 } catch (BadPaddingException e) {
 System.out.println(methodName + " Exception::" + e.getMessage());
 } catch (NumberFormatException e) {
 System.out.println(methodName + " Exception::" + e.getMessage());
 }

 return decryptedString;
 }
}



Swarup Reddy (13 Posts)

Founder of Developprojects.com. He is an Oracle certified expert java developer. He is proficient in end-to-end development right from requirement analysis to system study, designing, coding, testing, de- bugging, documentation and implementation. His areas of interests are Information Security, Programming, compiler designing and automation.