AES
The Advanced Encryption Standard (AES) is a block cipher encryption and decryption algorithm, the most used encryption algorithm in the worldwide. The AES processes block of 128 bits using a secret key of 128, 192, or 256 bits. It is used for encryption and decryption of a string.
Exmaple
/*********************************************************************************************
Author : Deepak Kumar Panda
Purpose : To encrypt and decrypt the given data with a strong key
Algorithm Used : AES/CBC/PKCS5Padding
METHODS & PARAMS :-
----------------
1. private SecretKeySpec prepareKey(String myPassword)
2. public String getEncryptedText(String strToEncrypt, ArrayList <String>vErrorObjects)
3. private String encrypt(String strToEncrypt, SecretKeySpec secretKey, ArrayList <String>vErrorObjects)
4. public String getDecryptedText(String strToDecrypt, ArrayList <String>vErrorObjects)
5. private String decrypt(String strToDecrypt, SecretKeySpec secretKey, ArrayList <String>vErrorObjects)
6. public static void main(java.lang.String[]) throws java.lang.Exception;
***********************************************************************************************/
Author : Deepak Kumar Panda
Purpose : To encrypt and decrypt the given data with a strong key
Algorithm Used : AES/CBC/PKCS5Padding
METHODS & PARAMS :-
----------------
1. private SecretKeySpec prepareKey(String myPassword)
2. public String getEncryptedText(String strToEncrypt, ArrayList <String>vErrorObjects)
3. private String encrypt(String strToEncrypt, SecretKeySpec secretKey, ArrayList <String>vErrorObjects)
4. public String getDecryptedText(String strToDecrypt, ArrayList <String>vErrorObjects)
5. private String decrypt(String strToDecrypt, SecretKeySpec secretKey, ArrayList <String>vErrorObjects)
6. public static void main(java.lang.String[]) throws java.lang.Exception;
***********************************************************************************************/
package com.els.core;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
/*********************************************************
iGBM Request/Response value encryption using AES algorithm
*********************************************************/
public class AESEncryptionTest
{
private static SecretKeySpec myKeySpec ;
private static final String keyAlgorithm = "AES";
private static final String encryptAlgorithm = "AES/CBC/PKCS5Padding";
//private static final String secKeyFactoryAlgorithm = "PBKDF2WithHmacSHA1";
private static final byte[] iv = { 0, 1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1 ,0};
//always prefer get passwordKey from properties or database combination of character or special symbols
private static final String strPsswordKey = "NKlfv13eZvz4MwIhEkOPIftxny+5fdVxgcfDiJVfHl8=";
/*****************************************************
Purpose : prepare secret key for encryption/decryption
******************************************************/
private SecretKeySpec prepareKey(String myPassword)
{
SecretKeySpec myKey = null;
MessageDigest shaMD = null;
try
{
//converting password to byte array
byte[] passKey = myPassword.getBytes("UTF-8");
//hashing key with SHA
shaMD = MessageDigest.getInstance("SHA-256");
byte[] shaKey = shaMD.digest(passKey);
//use only first 128 bit
byte[] key = Arrays.copyOf(shaKey, 16);
// prepage secret key with algorithm
myKey = new SecretKeySpec(key, keyAlgorithm);
//System.out.println("*****myKey :"+myKey);
} catch (NoSuchAlgorithmException ne) {
//System.out.println("NoSuchAlgorithmException @ prepareKey() :",ne.getMessage());
ne.printStackTrace();
} catch (UnsupportedEncodingException ue) {
//System.out.println("UnsupportedEncodingException @ prepareKey() :",ue.getMessage());
ue.printStackTrace();
} catch (Exception e) {
//System.out.println("Exception @ prepareKey() :",e.getMessage());
e.printStackTrace();
}
return myKey;
}//prepareKey()
/*****************************************************************
Purpose : Return Encrypted text and it's Key for a given String
******************************************************************/
public String getEncryptedText(String strToEncrypt, ArrayList <String>vErrorObjects)
{
String encryptedString = null;
try
{
myKeySpec = prepareKey(strPsswordKey);
encryptedString = encrypt(strToEncrypt.trim(), myKeySpec, vErrorObjects);
//System.out.println("******* getEncryptedText() @ encryptedString ="+encryptedString);
}
catch (Exception e)
{
vErrorObjects.add(e.getMessage());
//System.out.println("Exception @ getEncryptedText() :"+e.getMessage());
e.printStackTrace();
}
return encryptedString;
}//getEncryptedText()
/*****************************************************************
Purpose : encrypt the given text String with specific key based on AES
******************************************************************/
private String encrypt(String strToEncrypt, SecretKeySpec secretKey, ArrayList <String>vErrorObjects)
{
String sEncryptedString = "";
try
{
IvParameterSpec ivspec = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance(encryptAlgorithm);
cipher.init(Cipher.ENCRYPT_MODE, secretKey,ivspec);
sEncryptedString = Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
//System.out.println("*****sEncryptedString :"+sEncryptedString);
}
catch (Exception e)
{
vErrorObjects.add(e.getMessage());
//System.out.println("Exception @ encrypt() :",e.getMessage());
e.printStackTrace();
}
return sEncryptedString;
}//encrypt()
/**********************************************************************
Purpose : Return Decrypted String for a given Encrypted text and its Key
**********************************************************************/
public String getDecryptedText(String strToDecrypt, ArrayList <String>vErrorObjects)
{
String decryptedString = null;
try
{
myKeySpec = prepareKey(strPsswordKey);
//System.out.println("*******myKeySpec="+myKeySpec);
decryptedString = decrypt(strToDecrypt, myKeySpec, vErrorObjects);
//System.out.println("******* getDecryptedText() @ decryptedString ="+decryptedString);
}
catch (Exception e)
{
vErrorObjects.add(e.getMessage());
//System.out.println("Exception @ getDecryptedText() :",e.getMessage());
e.printStackTrace();
}
return decryptedString;
}//getDecryptedText()
/*****************************************************************
Purpose : decrypt the given Encrypted String with specific key based on AES
******************************************************************/
private String decrypt(String strToDecrypt, SecretKeySpec secretKey, ArrayList <String>vErrorObjects)
{
String sDecryptedString = "";
try
{
IvParameterSpec ivspec = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance(encryptAlgorithm);
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivspec);
sDecryptedString = new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt.getBytes("UTF-8"))));
//System.out.println("*****sDecryptedString :"+sDecryptedString);
}
catch(BadPaddingException bpe)
{
vErrorObjects.add(bpe.getMessage());
//System.out.println("BadPaddingException @ decrypt() :",bpe.getMessage());
bpe.printStackTrace();
}
catch (Exception e)
{
vErrorObjects.add(e.getMessage());
//System.out.println("Exception @ decrypt() :",e.getMessage());
e.printStackTrace();
}
return sDecryptedString;
}//decrypt()
/***********************
Main method for testing
************************/
public static void main (String []args) throws Exception
{
String encryptedText;
String decryptedText;
String requestText = "I love You";
IGBMAESEncryptionTest encryptObject = new IGBMAESEncryptionTest();
ArrayList <String>alErrorObjects = new ArrayList<String>();
System.out.println("\n\n#####################################################################\n\n");
System.out.println("Please Choose from following options : \n");
System.out.println("1) Encryption");
System.out.println("2) Decryption");
System.out.println("3) Quit");
System.out.println("\n\n#####################################################################\n\n");
Scanner sc = new Scanner(System.in);
System.out.print("Enter Option No : ");
String sOptions = sc.nextLine();
while(sOptions.equalsIgnoreCase("1")||sOptions.equalsIgnoreCase("2"))
{
if (sOptions.equalsIgnoreCase("1")){
System.out.print("\n\nPlease Enter Text to encrypt : ");
requestText = sc.nextLine();
System.out.println("\n\n#####################################################################\n\n");
System.out.println("Your input Text\t: "+requestText);
encryptedText = encryptObject.getEncryptedText(requestText,alErrorObjects);
if(!alErrorObjects.isEmpty()){
System.out.println("Failed ! Error Object Details ="+alErrorObjects);
}else{
System.out.println("Encrypted Text\t: "+encryptedText);
}
}else if (sOptions.equalsIgnoreCase("2")){
System.out.print("\n\nPlease Enter Encrypted Text to decrypt : ");
requestText = sc.nextLine();
System.out.println("\n\n#####################################################################\n\n");
System.out.println("Your input Text\t: "+requestText);;
decryptedText = encryptObject.getDecryptedText(requestText,alErrorObjects);
if(!alErrorObjects.isEmpty()){
System.out.println("Failed ! Error Object Details ="+alErrorObjects);
}else{
System.out.println("Decrypted Text\t: "+decryptedText);
}
}else{
System.exit(0);
}
System.out.println("\n\n#####################################################################\n\n");
System.out.print("If you want to continue ! Enter Option No : ");
sOptions = sc.nextLine();
}
}
}
No comments:
Post a Comment
Please share your feedback