· 6 years ago · Mar 08, 2019, 11:32 AM
1package id.co.sat.helper;
2
3import java.security.SecureRandom;
4
5import javax.crypto.Cipher;
6import javax.crypto.KeyGenerator;
7import javax.crypto.SecretKey;
8import javax.crypto.spec.SecretKeySpec;
9
10/**
11 * Created by Asus User on 22/11/2017.
12 */
13
14public class AESEncrypt {
15
16 public String encryption(String strNormalText){
17 String seedValue = "4Learning";
18 String normalTextEnc="";
19 try {
20 normalTextEnc = encrypt(seedValue, strNormalText);
21 } catch (Exception e) {
22 e.printStackTrace();
23 }
24 return normalTextEnc;
25 }
26 public String decryption(String strEncryptedText){
27 String seedValue = "4Learning";
28 String strDecryptedText="";
29 try {
30 strDecryptedText = decrypt(seedValue, strEncryptedText);
31 } catch (Exception e) {
32 e.printStackTrace();
33 }
34 return strDecryptedText;
35 }
36
37 public static String encrypt(String seed, String cleartext) throws Exception {
38 byte[] rawKey = getRawKey(seed.getBytes());
39 byte[] result = encrypt(rawKey, cleartext.getBytes());
40 return toHex(result);
41 }
42
43 public static String decrypt(String seed, String encrypted) throws Exception {
44 byte[] rawKey = getRawKey(seed.getBytes());
45 byte[] enc = toByte(encrypted);
46 byte[] result = decrypt(rawKey, enc);
47 return new String(result);
48 }
49
50 private static byte[] getRawKey(byte[] seed) throws Exception {
51 KeyGenerator kgen = KeyGenerator.getInstance("AES");
52 SecureRandom sr = SecureRandom.getInstance("SHA1PRNG","Crypto");
53 sr.setSeed(seed);
54 kgen.init(128, sr); // 192 and 256 bits may not be available
55 SecretKey skey = kgen.generateKey();
56 byte[] raw = skey.getEncoded();
57 return raw;
58 }
59
60
61 private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
62 SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
63 Cipher cipher = Cipher.getInstance("AES");
64 cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
65 byte[] encrypted = cipher.doFinal(clear);
66 return encrypted;
67 }
68
69 private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
70 SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
71 Cipher cipher = Cipher.getInstance("AES");
72 cipher.init(Cipher.DECRYPT_MODE, skeySpec);
73 byte[] decrypted = cipher.doFinal(encrypted);
74 return decrypted;
75 }
76
77 public static byte[] toByte(String hexString) {
78 int len = hexString.length()/2;
79 byte[] result = new byte[len];
80 for (int i = 0; i < len; i++)
81 result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();
82 return result;
83 }
84
85 public static String toHex(byte[] buf) {
86 if (buf == null)
87 return "";
88 StringBuffer result = new StringBuffer(2*buf.length);
89 for (int i = 0; i < buf.length; i++) {
90 appendHex(result, buf[i]);
91 }
92 return result.toString();
93 }
94
95 private final static String HEX = "0123456789ABCDEF";
96 private static void appendHex(StringBuffer sb, byte b) {
97 sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
98 }
99}