chenjiahe
5 天以前 826b66207dafbce24f441cb83fed1b241a6fba27
提交 | 用户 | age
826b66 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;
9 import java.util.regex.Pattern;
10
11 /**
12  * mybatis数据库的AES_ENCRYPT(加密)和AES_DECRYPT(解密)
13  * mybatis数据库的HEX(加壳)UNHEX(解壳)
14  * @author CJH
15  * @Date 2021-01-06
16  */
17 public class MysqlHexAesTool {
18
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
37      * @return 解密数据
38      */
39     public static String decryptData(String data,String aesKey,String encoding) {
40         try{
41             if(data == null){
42                 return null;
43             }
44             if(!isHexStrValid(data)){
45                 return data;
46             }
47             if(StringUtils.isEmpty(encoding)){
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){
55             throw new RuntimeException(e);
56         }
57         return data;
58     }
59
60     /**AES加密
61      * @param data 需要解密的数据
62      * @param aesKey 秘钥
63      * @param encoding 编码,不填默认UTF-8
64      * @return 返回大写加密的数据
65      */
66     public static String encryptData(String data,String aesKey,String encoding) {
67         try {
68             if(data == null){
69                 return null;
70             }
71
72             if (StringUtils.isEmpty(encoding)) {
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) {
85             throw new RuntimeException(e);
86         }
87         return data;
88     }
89
90     /**
91      * 判断是不是十六进制的字符串(字母是大写的)
92      * @param str 需要判断的字符串
93      * @return true or false
94      */
95     public static boolean isHexStrValid(String str) {
96
97         if(str == null){
98             return false;
99         }
100         //是否32位倍数
101         if(str.length()%32 != 0){
102             return false;
103         }
104         String pattern = "^[0-9A-F]+$";
105         return Pattern.compile(pattern).matcher(str).matches();
106     }
107
108
109
110 }