zhangxu
2022-02-10 0de2edd06892770c5c4562e6a9118b2fb9806738
提交 | 用户 | age
41d89b 1 package com.hx.util.mysql.aes;
C 2
3 import com.hx.util.StringUtils;
4 import org.apache.commons.codec.binary.Hex;
5
6 import javax.crypto.Cipher;
7 import javax.crypto.spec.SecretKeySpec;
8 import java.io.UnsupportedEncodingException;
8b0a46 9 import java.util.regex.Pattern;
41d89b 10
C 11 /**
12  * mybatis数据库的AES_ENCRYPT(加密)和AES_DECRYPT(解密)
13  * mybatis数据库的HEX(加壳)UNHEX(解壳)
14  * @author CJH
15  * @Date 2021-01-06
16  */
893a03 17 public class MysqlHexAesTool {
41d89b 18
C 19
20     public static SecretKeySpec generateMySQLAESKey(final String key, final String encoding) {
21         try {
22             final byte[] finalKey = new byte[16];
23             int i = 0;
24             for(byte b : key.getBytes(encoding)) {
25                 finalKey[i++%16] ^= b;
26             }
27             return new SecretKeySpec(finalKey, "AES");
28         } catch(UnsupportedEncodingException e) {
29             throw new RuntimeException(e);
30         }
31     }
32
33     /**AES 解密
34      * @param data 需要解密的数据
35      * @param aesKey 秘钥
36      * @param encoding 编码,不填默认UTF-8
e5bae5 37      * @return 解密数据
41d89b 38      */
e5bae5 39     public static String decryptData(String data,String aesKey,String encoding) {
C 40         try{
b39b1f 41             if(data == null){
C 42                 return null;
43             }
0de2ed 44             if(!isHexStrValid(data)){
Z 45                 return data;
46             }
e5bae5 47             if(StringUtils.isEmpty(encoding)){
C 48                 encoding = "UTF-8";
49             }
50             // Decrypt
51             final Cipher decryptCipher = Cipher.getInstance("AES");
52             decryptCipher.init(Cipher.DECRYPT_MODE, generateMySQLAESKey(aesKey, encoding));
53             data = new String(decryptCipher.doFinal(Hex.decodeHex(data.toCharArray())));
54         }catch (Exception e){
99a29e 55             throw new RuntimeException(e);
41d89b 56         }
e5bae5 57         return data;
41d89b 58     }
C 59
60     /**AES加密
61      * @param data 需要解密的数据
62      * @param aesKey 秘钥
63      * @param encoding 编码,不填默认UTF-8
45549c 64      * @return 返回大写加密的数据
41d89b 65      */
e5bae5 66     public static String encryptData(String data,String aesKey,String encoding) {
C 67         try {
b39b1f 68             if(data == null){
C 69                 return null;
70             }
0de2ed 71
e5bae5 72             if (StringUtils.isEmpty(encoding)) {
C 73                 encoding = "UTF-8";
74             }
75             // Encrypt
76             final Cipher encryptCipher = Cipher.getInstance("AES");
77             encryptCipher.init(Cipher.ENCRYPT_MODE, generateMySQLAESKey(aesKey, encoding));
78             char[] code = Hex.encodeHex(encryptCipher.doFinal(data.getBytes(encoding)));
79             StringBuilder builder = new StringBuilder();
80             for (char d : code) {
81                 builder.append(d);
82             }
83             data = builder.toString().toUpperCase();
84         } catch (Exception e) {
99a29e 85             throw new RuntimeException(e);
41d89b 86         }
e5bae5 87         return data;
41d89b 88     }
C 89
8b0a46 90     /**
C 91      * 判断是不是十六进制的字符串(字母是大写的)
92      * @param str 需要判断的字符串
93      * @return true or false
94      */
95     public static boolean isHexStrValid(String str) {
b39b1f 96         if(str == null){
C 97             return false;
98         }
8b0a46 99         String pattern = "^[0-9A-F]+$";
C 100         return Pattern.compile(pattern).matcher(str).matches();
101     }
102
103
104
41d89b 105 }