| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- package com.persagy.fm.common.utils;
- import cn.hutool.crypto.Mode;
- import cn.hutool.crypto.Padding;
- 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.nio.charset.Charset;
- import java.nio.charset.StandardCharsets;
- /**
- * 借助于hutool工具类实现 AES 128/256加密,全局编码UTF-8
- *
- * @version 1.0.0
- * @author zhangqiankun
- * @date 2021-03-13 15:29:50
- */
- public class SecureAES {
-
- public static void main(String[] args) {
- SecureAES aes = new SecureAES("63499E35378AE1B0733E3FED7F780B68", "C0E7BD39B52A15C7");
- String encryptAccount = aes.encrypt("TEST", "PC", "AC123456789");
- System.out.println("account info encrypt: " + encryptAccount);
- JSONObject decrypt = aes.decrypt(encryptAccount + ".FSD45SSD1B5D56GB5DFBD");
- System.out.println("header token decrypt: " + decrypt.toJSONString());
- JSONObject account = aes.decryptAccount(encryptAccount);
- System.out.println("account info decrypt: " + account.toJSONString());
- }
-
- private AES aes;
-
- public static final Charset CHARSET_UTF_8 = StandardCharsets.UTF_8;
-
- /**
- * 默认AES/ECB/PKCS5Padding
- *
- * @param key 128或256位
- */
- public SecureAES (String key) {
- aes = SecureUtil.aes(key.getBytes(CHARSET_UTF_8));
- }
-
- /**
- * 默认AES/CTS/PKCS5Padding
- *
- * @param key 128或256位
- * @param iv 16字节
- */
- public SecureAES (String key, String iv) {
- aes = new AES(Mode.CTS, Padding.PKCS5Padding, key.getBytes(CHARSET_UTF_8), iv.getBytes(CHARSET_UTF_8));
- }
-
- /**
- * 加密为16进制
- *
- * @param content
- * @return
- */
- public String encryptHex(String content) {
- return aes.encryptHex(content, CHARSET_UTF_8);
- }
-
- /**
- * 解密
- *
- * @param content
- * @return
- */
- public String decryptStr(String content) {
- return aes.decryptStr(content, CHARSET_UTF_8);
- }
-
- /**
- * 加密为16进制,参数组装为json格式数据
- *
- * @param groupCode
- * @param appId
- * @param accountId
- * @return
- */
- 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);
- }
-
- /**
- * 解密
- *
- * @param headerToken token字符串
- * @return token内容,json格式
- */
- public JSONObject decrypt(String headerToken) {
- if (StringUtil.isBlank(headerToken)) {
- throw new AESDecryptException("token is null");
- }
- String[] tokens = headerToken.split("\\.");
- if (tokens.length != 2) {
- throw new AESDecryptException("token invalid parameter");
- }
- // 加密的账号信息
- String encryptAccount = tokens[0];
- return this.decryptAccount(encryptAccount);
- }
-
- /**
- * 解密
- *
- * @param encryptAccount
- * @return token内容,json格式
- */
- public JSONObject decryptAccount(String encryptAccount) {
- String decryptStr = this.decryptStr(encryptAccount);
- if (StringUtil.isBlank(decryptStr)) {
- throw new AESDecryptException("AES decrypt failure");
- }
- JSONObject object = JSONObject.parseObject(decryptStr);
- return object;
- }
-
- }
|