· 6 years ago · Sep 23, 2019, 06:50 AM
1package com.mmoney.common.crypto;
2
3import java.io.UnsupportedEncodingException;
4import java.security.InvalidAlgorithmParameterException;
5import java.security.InvalidKeyException;
6import java.security.Key;
7import java.security.MessageDigest;
8import java.security.NoSuchAlgorithmException;
9
10import javax.crypto.BadPaddingException;
11import javax.crypto.Cipher;
12import javax.crypto.IllegalBlockSizeException;
13import javax.crypto.NoSuchPaddingException;
14import javax.crypto.SecretKey;
15import javax.crypto.spec.IvParameterSpec;
16import javax.crypto.spec.SecretKeySpec;
17
18import org.apache.commons.codec.binary.Base64;
19
20import com.mmoney.exception.ApplicationException;
21import com.wingmoney.constant.ErrorCode;
22
23public class AESCipherEncrypter {
24 private static final String AES_KEY = "HG58YZ3CR9";
25
26 public static String encrypt(String orignalText) throws ApplicationException {
27
28 String encodedText = null;
29
30 try {
31 final MessageDigest md = MessageDigest.getInstance("SHA-256");
32 final byte[] digestOfPassword = md.digest(AES_KEY.getBytes("utf-8"));
33 final SecretKey key = new SecretKeySpec(digestOfPassword, "AES");
34
35 final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
36 final IvParameterSpec iv = new IvParameterSpec(new byte[16]);
37
38 cipher.init(Cipher.ENCRYPT_MODE, key, iv);
39
40 final byte[] plainTextBytes = orignalText.getBytes("utf-8");
41 final byte[] encodeTextBytes = cipher.doFinal(plainTextBytes);
42
43 encodedText = new Base64().encodeToString(encodeTextBytes);
44
45 } catch (NoSuchAlgorithmException |
46 UnsupportedEncodingException |
47 IllegalBlockSizeException |
48 InvalidKeyException |
49 BadPaddingException |
50 NoSuchPaddingException | InvalidAlgorithmParameterException e) {
51 throw new ApplicationException(ErrorCode.GENERAL_FAIL, e);
52 }
53
54 return encodedText;
55 }
56
57 public static String encryptWithKey(String strkey, String orignalText) throws ApplicationException {
58
59 String encodedText = null;
60
61 try {
62 final MessageDigest md = MessageDigest.getInstance("SHA-256");
63 final byte[] digestOfPassword = md.digest(strkey.getBytes("utf-8"));
64 final SecretKey key = new SecretKeySpec(digestOfPassword, "AES");
65
66 final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
67 final IvParameterSpec iv = new IvParameterSpec(new byte[16]);
68
69 cipher.init(Cipher.ENCRYPT_MODE, key, iv);
70
71 final byte[] plainTextBytes = orignalText.getBytes("utf-8");
72 final byte[] encodeTextBytes = cipher.doFinal(plainTextBytes);
73
74 encodedText = new Base64().encodeToString(encodeTextBytes);
75
76 } catch (NoSuchAlgorithmException |
77 UnsupportedEncodingException |
78 IllegalBlockSizeException |
79 InvalidKeyException |
80 BadPaddingException |
81 NoSuchPaddingException | InvalidAlgorithmParameterException e) {
82 throw new ApplicationException(ErrorCode.GENERAL_FAIL, e);
83 }
84
85 return encodedText;
86 }
87
88 private static String encryptWithKey128(String strKey, String message) throws ApplicationException {
89 String encodedText = null;
90 try {
91 byte[] keyBytes = strKey.getBytes();
92 Key key = new SecretKeySpec(keyBytes, "AES");
93 Cipher c = Cipher.getInstance("AES");
94 c.init(Cipher.ENCRYPT_MODE, key);
95 final byte[] encodeTextBytes = c.doFinal(message.getBytes());
96 encodedText = new Base64().encodeToString(encodeTextBytes);
97 }catch (NoSuchAlgorithmException |
98 IllegalBlockSizeException |
99 InvalidKeyException |
100 BadPaddingException |
101 NoSuchPaddingException e) {
102 throw new ApplicationException(ErrorCode.GENERAL_FAIL, e);
103 }
104 return encodedText;
105 }
106
107 public static String decryptWithKey128(String strKey, String orignalText) throws ApplicationException {
108 try {
109 byte[] keyBytes = strKey.getBytes();
110 Key key = new SecretKeySpec(keyBytes, "AES");
111 Cipher c = Cipher.getInstance("AES");
112 c.init(Cipher.DECRYPT_MODE, key);
113 byte[] plainTextBytes = Base64.decodeBase64(orignalText);
114 byte[] decValue = c.doFinal(plainTextBytes);
115 String decryptedValue = new String(decValue);
116 return decryptedValue;
117 } catch (NoSuchAlgorithmException |
118 IllegalBlockSizeException |
119 InvalidKeyException |
120 BadPaddingException |
121 NoSuchPaddingException e) {
122 throw new ApplicationException(ErrorCode.GENERAL_FAIL, e);
123 }
124 }
125
126 public static String decrypt(String orignalText) throws ApplicationException {
127
128 try {
129 final MessageDigest md = MessageDigest.getInstance("SHA-256");
130 final byte[] digestOfPassword = md.digest(AES_KEY.getBytes("utf-8"));
131 final SecretKey key = new SecretKeySpec(digestOfPassword, "AES");
132
133 final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
134
135 cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(new byte[16]));
136 final byte[] plainTextBytes = Base64.decodeBase64(orignalText);
137 final byte[] encodeTextBytes = cipher.doFinal(plainTextBytes);
138
139 return new String(encodeTextBytes);
140
141 } catch (NoSuchAlgorithmException |
142 UnsupportedEncodingException |
143 IllegalBlockSizeException |
144 InvalidKeyException |
145 BadPaddingException |
146 NoSuchPaddingException | InvalidAlgorithmParameterException e) {
147 throw new ApplicationException(ErrorCode.GENERAL_FAIL, e);
148 }
149 }
150
151 public static void main(String[] args) throws ApplicationException, Exception {
152 String key = "MZygpewJsCpRrfOr";
153 System.out.println(AESCipherEncrypter.encryptWithKey128(key, "Hello"));
154 System.out.println(AESCipherEncrypter.decryptWithKey128(key, "3bjY8L53hqixcyf7DQ9/vnpD5G7xA+4vbTFMglmv4hBr/tMp7mQSb+5Lk36uD4lLjcR2g36Nwwox\r\ngcwsHHTRwAiRiVmCWxsSGzjZnslb8+5d3YV7PEyYzWmvigkn7Fo4LXnil6KjTr7NgXYS0XmyngR0\r\nDticZ1aS307k9q/vvn2UWeWxX5DkmflyOqGc0nCYy0MFDeWspN8NDyFy24ggxg=="));
155 System.out.println(AESCipherEncrypter.decrypt("9b2mVXvsmIZJfm4SF0M2xw=="));
156 //System.out.println(AESCipherEncrypter.encrypt("Hello"));*/
157
158 System.out.println(AESCipherEncrypter.encrypt("encryptWithKey"));
159 }
160
161
162}