Encode Decode String Using Aes

Basic example of encode and decode a string using AES:

Code:

package foo;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

public class EncryptDecryptTest {
 
 public static Cipher getCipher(
           String synchro1, String synchro2, String synchro3, String synchro4,boolean isEncryptMode)
 throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException
 {
  byte raw[] = (synchro1 + synchro2 + synchro3 + synchro4).getBytes();
  SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
  Cipher cipher = Cipher.getInstance("AES");
  if(isEncryptMode){
   cipher.init(Cipher.ENCRYPT_MODE,skeySpec);
  }
  else{
   cipher.init(Cipher.DECRYPT_MODE,skeySpec);
  }  
  return cipher;
 } 

 public static byte[] hexToByte(String hex)
 {
  byte bts[] = new byte[hex.length() / 2];
  for(int i = 0; i < bts.length; i++){
   bts[i] = (byte)Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);
  }

  return bts;
 }

 public static String toHexString(byte bytes[])
 {
  StringBuffer retString = new StringBuffer();
  for(int i = 0; i < bytes.length; i++)
   retString.append(Integer.toHexString(256 + (bytes[i] & 0xff)).substring(1));

  return retString.toString();
 }

 public static String encrypt(String text)
              throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException{
  javax.crypto.Cipher cipher = getCipher("bb5","1860", "17a74", "213f",true);
  return toHexString(cipher.doFinal(text.getBytes()));
 }

 public static String decrypt(String text)
              throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException{
  javax.crypto.Cipher cipher = getCipher("bb5","1860", "17a74", "213f",false);
  String st=new String(cipher.doFinal(hexToByte(text)));  
  return st;
 }

 public static void main(String []str) throws Exception{
  String secretText="javalearning";
  String enSecretText=encrypt(secretText);

  System.out.println("\""+secretText+"\" after encryption---->"+enSecretText);

  String deSecretText=decrypt(enSecretText);
  System.out.println("After decryption --->"+deSecretText);
 }
}

Notes:

Check the main. It encodes the string "javalearning" and decode it back. 

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.