· 6 years ago · Apr 07, 2019, 05:22 AM
1package com.who2sell.utils;
2
3import java.io.UnsupportedEncodingException;
4import java.security.InvalidAlgorithmParameterException;
5import java.security.InvalidKeyException;
6import java.security.NoSuchAlgorithmException;
7import java.security.spec.InvalidKeySpecException;
8import java.security.spec.KeySpec;
9
10import javax.crypto.BadPaddingException;
11import javax.crypto.Cipher;
12import javax.crypto.IllegalBlockSizeException;
13import javax.crypto.NoSuchPaddingException;
14import javax.crypto.SecretKey;
15import javax.crypto.SecretKeyFactory;
16import javax.crypto.spec.IvParameterSpec;
17import javax.crypto.spec.PBEKeySpec;
18import javax.crypto.spec.SecretKeySpec;
19
20import org.apache.tomcat.util.buf.HexUtils;
21
22/**
23 * Util Class helps to encrypt and decrypt data based on AES 256 bit based
24 * encryption
25 *
26 * @author Abhishek
27 *
28 */
29public class AESEncryptionUtil {
30
31 private static final String password = "thisispipechimpsmailypassword99";
32 private static final String ivStr = "807ba7ddeab2b8ea6af5e6e7eff13911";
33 private static final byte[] salt = new byte[] { (byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32, (byte) 0x56,
34 (byte) 0x35, (byte) 0xE3, (byte) 0x03 };
35
36 private static SecretKey secret;
37 public static void main(String[] args) throws Exception {
38
39 secret = getKey();
40 String enc = encrypt("vishal@patel");
41 String plain = decrypt(enc);
42 System.out.println(enc + plain);
43 }
44
45 private static SecretKey getKey() throws NoSuchAlgorithmException, InvalidKeySpecException {
46
47 SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
48 KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 65536, 256);
49 SecretKey tmp = factory.generateSecret(spec);
50 return new SecretKeySpec(tmp.getEncoded(), "AES");
51 }
52
53 /** Encrypts the plain text data **/
54 public static String encrypt(String plainText) throws NoSuchAlgorithmException, InvalidKeySpecException,
55 NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException,
56 BadPaddingException, UnsupportedEncodingException {
57
58 // SecretKey secret = getKey();
59 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
60 byte[] iv = HexUtils.fromHexString(ivStr);
61 cipher.init(Cipher.ENCRYPT_MODE, secret, new IvParameterSpec(iv));
62 byte[] plainData = "Hello, World!".getBytes("UTF-8");
63 byte[] encryptedText = cipher.doFinal(plainData);
64 return new String(encryptedText, "UTF-8");
65 }
66
67 /** Decrypts to plain text data from Encrypted one **/
68 public static String decrypt(String encryptedText) throws NoSuchAlgorithmException, InvalidKeySpecException,
69 NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException,
70 BadPaddingException, UnsupportedEncodingException {
71
72 // SecretKey secret = getKey();
73 Cipher cipher2 = Cipher.getInstance("AES/CBC/PKCS5Padding");
74 byte[] iv = HexUtils.fromHexString(ivStr);
75 cipher2.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv));
76 byte[] encData = cipher2.doFinal(encryptedText.getBytes("UTF-8"));
77 String plainText = new String(encData, "UTF-8");
78 return plainText;
79 }
80}