提交 | 用户 | 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 |
} |
e5bae5
|
44 |
if(StringUtils.isEmpty(encoding)){ |
C |
45 |
encoding = "UTF-8"; |
|
46 |
} |
|
47 |
// Decrypt |
|
48 |
final Cipher decryptCipher = Cipher.getInstance("AES"); |
|
49 |
decryptCipher.init(Cipher.DECRYPT_MODE, generateMySQLAESKey(aesKey, encoding)); |
|
50 |
data = new String(decryptCipher.doFinal(Hex.decodeHex(data.toCharArray()))); |
|
51 |
}catch (Exception e){ |
99a29e
|
52 |
throw new RuntimeException(e); |
41d89b
|
53 |
} |
e5bae5
|
54 |
return data; |
41d89b
|
55 |
} |
C |
56 |
|
|
57 |
/**AES加密 |
|
58 |
* @param data 需要解密的数据 |
|
59 |
* @param aesKey 秘钥 |
|
60 |
* @param encoding 编码,不填默认UTF-8 |
45549c
|
61 |
* @return 返回大写加密的数据 |
41d89b
|
62 |
*/ |
e5bae5
|
63 |
public static String encryptData(String data,String aesKey,String encoding) { |
C |
64 |
try { |
b39b1f
|
65 |
if(data == null){ |
C |
66 |
return null; |
|
67 |
} |
e5bae5
|
68 |
if (StringUtils.isEmpty(encoding)) { |
C |
69 |
encoding = "UTF-8"; |
|
70 |
} |
|
71 |
// Encrypt |
|
72 |
final Cipher encryptCipher = Cipher.getInstance("AES"); |
|
73 |
encryptCipher.init(Cipher.ENCRYPT_MODE, generateMySQLAESKey(aesKey, encoding)); |
|
74 |
char[] code = Hex.encodeHex(encryptCipher.doFinal(data.getBytes(encoding))); |
|
75 |
StringBuilder builder = new StringBuilder(); |
|
76 |
for (char d : code) { |
|
77 |
builder.append(d); |
|
78 |
} |
|
79 |
data = builder.toString().toUpperCase(); |
|
80 |
} catch (Exception e) { |
99a29e
|
81 |
throw new RuntimeException(e); |
41d89b
|
82 |
} |
e5bae5
|
83 |
return data; |
41d89b
|
84 |
} |
C |
85 |
|
8b0a46
|
86 |
/** |
C |
87 |
* 判断是不是十六进制的字符串(字母是大写的) |
|
88 |
* @param str 需要判断的字符串 |
|
89 |
* @return true or false |
|
90 |
*/ |
|
91 |
public static boolean isHexStrValid(String str) { |
b39b1f
|
92 |
if(str == null){ |
C |
93 |
return false; |
|
94 |
} |
8b0a46
|
95 |
String pattern = "^[0-9A-F]+$"; |
C |
96 |
return Pattern.compile(pattern).matcher(str).matches(); |
|
97 |
} |
|
98 |
|
|
99 |
|
|
100 |
|
41d89b
|
101 |
} |