· 6 years ago · Jul 29, 2019, 10:26 PM
1// 1. Javascripts
2
3var iv = '00000000000000000000000000000000';
4var salt = '99990000000000000000000000000099';
5
6var AesUtil = function() {
7 this.keySize = 128 / 32;
8 this.iterationCount = 1000;
9};
10
11AesUtil.prototype.generateKey = function(salt, passPhrase) {
12 var key = CryptoJS.PBKDF2(
13 passPhrase,
14 CryptoJS.enc.Hex.parse(salt),
15 {keySize: this.keySize, iterations: this.iterationCount});
16 return key;
17};
18
19AesUtil.prototype.encrypt = function(passPhrase, plainText) {
20 var key = this.generateKey(salt, passPhrase);
21 var encrypted = CryptoJS.AES.encrypt(
22 plainText,
23 key,
24 {iv: CryptoJS.enc.Hex.parse(iv)});
25 return encrypted.ciphertext.toString(CryptoJS.enc.Base64);
26};
27
28AesUtil.prototype.decrypt = function(passPhrase, cipherText) {
29 var key = this.generateKey(salt, passPhrase);
30 var cipherParams = CryptoJS.lib.CipherParams.create({
31 ciphertext: CryptoJS.enc.Base64.parse(cipherText),
32 });
33 var decrypted = CryptoJS.AES.decrypt(
34 cipherParams,
35 key,
36 {iv: CryptoJS.enc.Hex.parse(iv)});
37 return decrypted.toString(CryptoJS.enc.Utf8);
38};
39
40// usage
41var aesUtil = new AesUtil();
42var hashKey = Sha256.hash('' + key);
43var rawData = aesUtil.decrypt(hashKey, encryptData);
44
45
46// 2. Java
47public class AesUtil {
48 private final int keySize = 128;
49 private final int iterationCount = 1000;
50 private final Cipher cipher;
51
52
53 public AesUtil() throws NoSuchPaddingException, NoSuchAlgorithmException {
54 cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
55 }
56
57 public String encrypt(String salt, String iv, String passphrase, String plaintext) throws Exception {
58 SecretKey key = generateKey(salt, passphrase);
59 byte[] encrypted = doFinal(Cipher.ENCRYPT_MODE, key, iv, plaintext.getBytes("UTF-8"));
60 return base64(encrypted);
61
62 }
63
64 public String decrypt(String salt, String iv, String passphrase, String ciphertext) throws Exception {
65 SecretKey key = generateKey(salt, passphrase);
66 byte[] decrypted = doFinal(Cipher.DECRYPT_MODE, key, iv, base64(ciphertext));
67 return new String(decrypted, "UTF-8");
68 }
69
70 private byte[] doFinal(int encryptMode, SecretKey key, String iv, byte[] bytes) throws Exception {
71 cipher.init(encryptMode, key, new IvParameterSpec(hex(iv)));
72 return cipher.doFinal(bytes);
73 }
74
75 private SecretKey generateKey(String salt, String passphrase) throws Exception {
76 SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
77 KeySpec spec = new PBEKeySpec(passphrase.toCharArray(), hex(salt), iterationCount, keySize);
78 SecretKey key = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES");
79 return key;
80 }
81
82 public static String random(int length) {
83 byte[] salt = new byte[length];
84 new SecureRandom().nextBytes(salt);
85 return hex(salt);
86 }
87
88 public static String base64(byte[] bytes) {
89 return Base64.encodeBase64String(bytes);
90 }
91
92 public static byte[] base64(String str) {
93 return Base64.decodeBase64(str);
94 }
95
96 public static String hex(byte[] bytes) {
97 return Hex.encodeHexString(bytes);
98 }
99
100 public static byte[] hex(String str) {
101 try {
102 return Hex.decodeHex(str.toCharArray());
103 } catch (DecoderException e) {
104 throw new IllegalStateException(e);
105 }
106 }
107}
108
109// usage
110
111 private static String iv = "00000000000000000000000000000000";
112 private static String salt = "99990000000000000000000000000099";
113
114 public String encrypt(String key, String rawData) {
115 try {
116 String hashKey = DigestUtils.sha256Hex(key);
117 AesUtil aesUtil = new AesUtil();
118 String plaintext = aesUtil.encrypt(salt, iv, hashKey, rawData);
119 return plaintext;
120 } catch (Exception e) {
121 // handle exeption
122 }
123 }
124
125 public String decrypt(String key, String encryptData) {
126 try {
127 String hashKey = DigestUtils.sha256Hex(key);
128 AesUtil aesUtil = new AesUtil();
129 String plaintext = aesUtil.decrypt(salt, iv, hashKey, encryptData);
130 return plaintext;
131 } catch (Exception e) {
132 // handle exeption
133 }
134 }