Monday, March 17, 2014

java encrypt de-crypt example

    import javax.crypto.Cipher;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;
   
    import org.apache.commons.codec.binary.Base64;
   
    public class Encryptor {
    public static String encrypt(String key1, String key2, String value) {
    try {
    IvParameterSpec iv = new IvParameterSpec(key2.getBytes("UTF-8"));
   
    SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes("UTF-8"),
    "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
    byte[] encrypted = cipher.doFinal(value.getBytes());
    System.out.println("encrypted string:"
    + Base64.encodeBase64String(encrypted));
    return Base64.encodeBase64String(encrypted);
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    return null;
    }
   
    public static String decrypt(String key1, String key2, String encrypted) {
    try {
    IvParameterSpec iv = new IvParameterSpec(key2.getBytes("UTF-8"));
   
    SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes("UTF-8"),
    "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
    byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));
   
    return new String(original);
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    return null;
    }
   
    public static void main(String[] args) {
   
    String key1 = "Bar12345Bar12345"; // 128 bit key
    String key2 = "ThisIsASecretKet";
    System.out.println(decrypt(key1, key2,
    encrypt(key1, key2, "Hello World")));
    }
    }

1 comment:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.
    I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging.
    If anyone wants to become a Java developer learn from Java EE Online Training from India.
    or learn thru Java EE Online Training from India .
    Nowadays Java has tons of job opportunities on various vertical industry.

    ReplyDelete