提交 | 用户 | age
|
5c5945
|
1 |
package com.hx.util; |
E |
2 |
|
|
3 |
import org.apache.commons.codec.binary.Base64; |
|
4 |
|
|
5 |
import javax.crypto.Cipher; |
|
6 |
import javax.crypto.SecretKey; |
|
7 |
import javax.crypto.spec.SecretKeySpec; |
|
8 |
import java.io.UnsupportedEncodingException; |
|
9 |
|
|
10 |
/** |
|
11 |
* AES加密工具类 |
|
12 |
*/ |
|
13 |
public class Aes { |
|
14 |
|
|
15 |
/** |
|
16 |
* 将二进制转换成16进制 |
|
17 |
* |
|
18 |
* @param buf 字节数组 |
|
19 |
* @return |
|
20 |
*/ |
|
21 |
public static String parseByte2HexStr(byte buf[]) { |
|
22 |
StringBuffer sb = new StringBuffer(); |
|
23 |
for (int i = 0; i < buf.length; i++) { |
|
24 |
String hex = Integer.toHexString(buf[i] & 0xFF); |
|
25 |
if (hex.length() == 1) { |
|
26 |
hex = '0' + hex; |
|
27 |
} |
|
28 |
sb.append(hex.toUpperCase()); |
|
29 |
} |
|
30 |
return sb.toString(); |
|
31 |
} |
|
32 |
|
|
33 |
/** |
|
34 |
* 将16进制转换为二进制 |
|
35 |
* |
|
36 |
* @param hexStr 16进制字符 |
|
37 |
* @return |
|
38 |
*/ |
|
39 |
public static byte[] parseHexStr2Byte(String hexStr) { |
|
40 |
if (hexStr.length() < 1) |
|
41 |
return null; |
|
42 |
byte[] result = new byte[hexStr.length() / 2]; |
|
43 |
for (int i = 0; i < hexStr.length() / 2; i++) { |
|
44 |
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16); |
|
45 |
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16); |
|
46 |
result[i] = (byte) (high * 16 + low); |
|
47 |
} |
|
48 |
return result; |
|
49 |
} |
|
50 |
|
|
51 |
/** |
|
52 |
* 加密 |
|
53 |
* @param str 待加密字符串 |
|
54 |
* @param secret 密钥 |
|
55 |
* @return |
|
56 |
*/ |
|
57 |
public static byte[] encrypt(String str, String secret) { |
|
58 |
if (null != str) { |
|
59 |
byte[] bytes = null; |
|
60 |
|
|
61 |
try { |
|
62 |
Base64 base64 = new Base64(); |
|
63 |
String ALGORITHM = "AES"; |
|
64 |
SecretKey desKey = new SecretKeySpec(secret.getBytes("UTF-8"), ALGORITHM);// 生成密钥 |
|
65 |
Cipher c; |
|
66 |
c = Cipher.getInstance(ALGORITHM); |
|
67 |
c.init(Cipher.ENCRYPT_MODE, desKey); |
|
68 |
bytes = c.doFinal(str.getBytes("UTF-8")); |
|
69 |
return bytes; |
|
70 |
} catch (Exception e) { |
|
71 |
e.printStackTrace(); |
|
72 |
} |
|
73 |
|
|
74 |
} |
|
75 |
return null; |
|
76 |
} |
|
77 |
|
|
78 |
/** |
|
79 |
* 解密 |
|
80 |
* @param str 待解密字符串 |
|
81 |
* @param secret 密钥 |
|
82 |
* @return |
|
83 |
*/ |
|
84 |
public static byte[] decrypt(byte[] str, String secret) { |
|
85 |
try { |
|
86 |
String ALGORITHM = "AES"; |
|
87 |
SecretKey desKey = new SecretKeySpec(secret.getBytes("UTF-8"), ALGORITHM);// 生成密钥 |
|
88 |
Cipher c = Cipher.getInstance(ALGORITHM); |
|
89 |
c.init(Cipher.DECRYPT_MODE, desKey); |
|
90 |
return c.doFinal(str); |
|
91 |
} catch (Exception e) { |
|
92 |
e.printStackTrace(); |
|
93 |
} |
|
94 |
return null; |
|
95 |
} |
|
96 |
|
|
97 |
public static void main(String[] args) throws UnsupportedEncodingException { |
|
98 |
|
|
99 |
String content = "apiKey=phitabfaceapi&userId=o5W1it6Pvqn6vPX6E1BGERH8J1lw"; |
|
100 |
String password = "phitabfacesecret"; |
|
101 |
|
|
102 |
byte[] encode = encrypt(content, password); |
|
103 |
String code = parseByte2HexStr(encode); |
|
104 |
System.out.println("密文字符串:" + code); |
|
105 |
|
|
106 |
//16进制字符串转成字节数组 |
|
107 |
byte[] decode = parseHexStr2Byte(code); |
|
108 |
// 解密 |
|
109 |
byte[] decryptResult = decrypt(decode, password); |
|
110 |
System.out.println("解密后:" + new String(decryptResult, "UTF-8")); //不转码会乱码 |
|
111 |
|
|
112 |
} |
|
113 |
} |