· 6 years ago · Sep 01, 2019, 01:32 PM
1package com.wardana.sukma;
2
3import java.security.SecureRandom;
4
5import javax.crypto.BadPaddingException;
6import javax.crypto.Cipher;
7import javax.crypto.IllegalBlockSizeException;
8import javax.crypto.SecretKey;
9import javax.crypto.SecretKeyFactory;
10import javax.crypto.spec.IvParameterSpec;
11import javax.crypto.spec.PBEKeySpec;
12import javax.crypto.spec.SecretKeySpec;
13
14import org.apache.commons.codec.binary.Base64;
15
16public class AESAlgorithm {
17 private static final String TOKEN = "passwd";
18 private String salt;
19 private int pwdIterations = 65536;
20 private int keySize = 256;
21 private byte[] ivBytes;
22 private String keyAlgorithm = "AES";
23 private String encryptAlgorithm = "AES/CBC/PKCS5Padding";
24 private String secretKeyFactoryAlgorithm = "PBKDF2WithHmacSHA1";
25
26 public AESAlgorithm(){
27 this.salt = getSalt();
28 }
29
30 private String getSalt(){
31 SecureRandom random = new SecureRandom();
32 byte bytes[] = new byte[20];
33 random.nextBytes(bytes);
34 String text = new String(bytes);
35 return text;
36 }
37
38 /**
39 *
40 * @param plainText
41 * @return encrypted text
42 * @throws Exception
43 */
44 public String encyrpt(String plainText) throws Exception{
45 //generate key
46 byte[] saltBytes = salt.getBytes("UTF-8");
47
48 SecretKeyFactory skf = SecretKeyFactory.getInstance(this.secretKeyFactoryAlgorithm);
49 PBEKeySpec spec = new PBEKeySpec(TOKEN.toCharArray(), saltBytes, this.pwdIterations, this.keySize);
50 SecretKey secretKey = skf.generateSecret(spec);
51 SecretKeySpec key = new SecretKeySpec(secretKey.getEncoded(), keyAlgorithm);
52
53 //AES initialization
54 Cipher cipher = Cipher.getInstance(encryptAlgorithm);
55 cipher.init(Cipher.ENCRYPT_MODE, key);
56
57 //generate IV
58 this.ivBytes = cipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV();
59 byte[] encryptedText = cipher.doFinal(plainText.getBytes("UTF-8"));
60 return new Base64().encodeAsString(encryptedText);
61 }
62
63 /**
64 *
65 * @param encryptText
66 * @return decrypted text
67 * @throws Exception
68 */
69 public String decrypt(String encryptText) throws Exception {
70 byte[] saltBytes = salt.getBytes("UTF-8");
71 byte[] encryptTextBytes = new Base64().decode(encryptText);
72
73 SecretKeyFactory skf = SecretKeyFactory.getInstance(this.secretKeyFactoryAlgorithm);
74 PBEKeySpec spec = new PBEKeySpec(TOKEN.toCharArray(), saltBytes, this.pwdIterations, this.keySize);
75 SecretKey secretKey = skf.generateSecret(spec);
76 SecretKeySpec key = new SecretKeySpec(secretKey.getEncoded(), keyAlgorithm);
77
78 //decrypt the message
79 Cipher cipher = Cipher.getInstance(encryptAlgorithm);
80 cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(ivBytes));
81
82 byte[] decyrptTextBytes = null;
83 try {
84 decyrptTextBytes = cipher.doFinal(encryptTextBytes);
85 } catch (IllegalBlockSizeException e) {
86 // TODO: handle exception
87 e.printStackTrace();
88 } catch (BadPaddingException e) {
89 e.printStackTrace();
90 }
91 String text = new String(decyrptTextBytes);
92 return text;
93 }
94}