package com.linghu.utils; import cn.afocus.crypt.sign.UserSign; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import java.nio.charset.StandardCharsets; @Component public class OpenCryptUtil { // 初始化加密配置(若需要) @PostConstruct public void init() { } /** * AES加密(假设 open-crypt 默认使用AES-CBC模式) */ public String encrypt(String user) { try { // 调用 open-crypt 的加密方法(根据实际API调整) String secret="fY9tX7vX7qH5bN2cD8eM4xD2fH7uL4xM"; JSONObject geoJSON = new JSONObject(); geoJSON.put("name",user); String token = UserSign.gen(geoJSON.toJSONString(),secret.getBytes(StandardCharsets.UTF_8)); return token; } catch (Exception e) { throw new RuntimeException("加密失败:" + e.getMessage(), e); } } /** * AES解密 */ public String decrypt(String token) { try { // 调用 open-crypt 的解密方法 byte[] keyBytes = "fY9tX7vX7qH5bN2cD8eM4xD2fH7uL4xM".getBytes(StandardCharsets.UTF_8); return UserSign.decrypt(token, keyBytes); } catch (Exception e) { throw new RuntimeException("解密失败:" + e.getMessage(), e); } } }