|
@@ -6,9 +6,11 @@ import cn.hutool.crypto.SecureUtil;
|
|
|
import cn.hutool.crypto.symmetric.AES;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.persagy.common.utils.StringUtil;
|
|
|
-import com.persagy.fm.common.constant.AppContextConstants;
|
|
|
import com.persagy.security.exception.AESDecryptException;
|
|
|
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.net.URLDecoder;
|
|
|
+import java.net.URLEncoder;
|
|
|
import java.nio.charset.Charset;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
@@ -16,16 +18,19 @@ import java.nio.charset.StandardCharsets;
|
|
|
* 借助于hutool工具类实现 AES 128/256加密,全局编码UTF-8
|
|
|
*
|
|
|
* @version 1.0.0
|
|
|
+ * @company persagy
|
|
|
* @author zhangqiankun
|
|
|
* @date 2021-03-13 15:29:50
|
|
|
*/
|
|
|
public class SecureAES {
|
|
|
|
|
|
- public static void main(String[] args) {
|
|
|
+ public static void main(String[] args) throws UnsupportedEncodingException {
|
|
|
SecureAES aes = new SecureAES("63499E35378AE1B0733E3FED7F780B68", "C0E7BD39B52A15C7");
|
|
|
- String encryptAccount = aes.encrypt("TEST", "PC", "AC123456789");
|
|
|
+ JSONObject object = new JSONObject();
|
|
|
+ object.put("groupCode", "TEST");
|
|
|
+ String encryptAccount = aes.encryptAccount(object);
|
|
|
System.out.println("account info encrypt: " + encryptAccount);
|
|
|
- JSONObject decrypt = aes.decrypt(encryptAccount + ".FSD45SSD1B5D56GB5DFBD");
|
|
|
+ JSONObject decrypt = aes.decryptToken(encryptAccount + ".FSD45SSD1B5D56GB5DFBD");
|
|
|
System.out.println("header token decrypt: " + decrypt.toJSONString());
|
|
|
JSONObject account = aes.decryptAccount(encryptAccount);
|
|
|
System.out.println("account info decrypt: " + account.toJSONString());
|
|
@@ -75,48 +80,60 @@ public class SecureAES {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 加密为16进制,参数组装为json格式数据
|
|
|
+ * 解密
|
|
|
*
|
|
|
- * @param groupCode
|
|
|
- * @param appId
|
|
|
- * @param accountId
|
|
|
+ * @param content
|
|
|
* @return
|
|
|
+ * @throws UnsupportedEncodingException
|
|
|
*/
|
|
|
- public String encrypt(String groupCode, String appId, String accountId) {
|
|
|
- JSONObject object = new JSONObject();
|
|
|
- object.put(AppContextConstants.GROUP_CODE, groupCode);
|
|
|
- object.put(AppContextConstants.APP_ID, appId);
|
|
|
- object.put(AppContextConstants.ACCOUNT_ID, accountId);
|
|
|
- return aes.encryptHex(object.toJSONString(), CHARSET_UTF_8);
|
|
|
+ public String decryptFromBase64(String content) throws UnsupportedEncodingException {
|
|
|
+ String decode = URLDecoder.decode(content, CHARSET_UTF_8.toString());
|
|
|
+ return aes.decryptStr(decode, CHARSET_UTF_8);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 解密
|
|
|
+ * 加密为16进制,参数组装为json格式数据
|
|
|
+ */
|
|
|
+ public String encryptToBase64(JSONObject object) throws UnsupportedEncodingException {
|
|
|
+ String encryptHex = aes.encryptHex(object.toJSONString(), CHARSET_UTF_8);
|
|
|
+ return URLEncoder.encode(encryptHex, CHARSET_UTF_8.toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 加密为16进制,参数组装为json格式数据,且经过URLEncoder编码
|
|
|
+ *
|
|
|
+ * @throws UnsupportedEncodingException
|
|
|
+ */
|
|
|
+ public String encryptAccount(JSONObject object) throws UnsupportedEncodingException {
|
|
|
+ String encryptHex = aes.encryptHex(object.toJSONString(), CHARSET_UTF_8);
|
|
|
+ return URLEncoder.encode(encryptHex, CHARSET_UTF_8.toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解密,且经过URLDecoder编码
|
|
|
*
|
|
|
- * @param headerToken token字符串
|
|
|
- * @return token内容,json格式
|
|
|
+ * @return
|
|
|
+ * @throws UnsupportedEncodingException
|
|
|
*/
|
|
|
- public JSONObject decrypt(String headerToken) {
|
|
|
+ public JSONObject decryptToken(String headerToken) throws UnsupportedEncodingException {
|
|
|
if (StringUtil.isBlank(headerToken)) {
|
|
|
throw new AESDecryptException("token is null");
|
|
|
}
|
|
|
String[] tokens = headerToken.split("\\.");
|
|
|
- if (tokens.length != 2) {
|
|
|
+ if (tokens.length < 2) {
|
|
|
throw new AESDecryptException("token invalid parameter");
|
|
|
}
|
|
|
- // 加密的账号信息
|
|
|
- String encryptAccount = tokens[0];
|
|
|
+ String encryptAccount = tokens[0]; // 加密的账号信息
|
|
|
return this.decryptAccount(encryptAccount);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 解密
|
|
|
*
|
|
|
- * @param encryptAccount
|
|
|
- * @return token内容,json格式
|
|
|
+ * @throws UnsupportedEncodingException
|
|
|
*/
|
|
|
- public JSONObject decryptAccount(String encryptAccount) {
|
|
|
- String decryptStr = this.decryptStr(encryptAccount);
|
|
|
+ public JSONObject decryptAccount(String encryptAccount) throws UnsupportedEncodingException {
|
|
|
+ String decryptStr = this.decryptFromBase64(encryptAccount);
|
|
|
if (StringUtil.isBlank(decryptStr)) {
|
|
|
throw new AESDecryptException("AES decrypt failure");
|
|
|
}
|