package com.moral.util; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; /** * @ClassName TokenEncryptUtils * @Description token加密、解密工具 * @Author 陈凯裕 * @Date 2021/3/10 11:50 * @Version TODO **/ @Component @Slf4j public class TokenEncryptUtils { private static String key; @Value("${TOKEN.KEY}") public void setKey(String tokenKey){ this.key = tokenKey; } /** * 加密 * @param str * @return */ public static String encoded(String str) { return strToHex(encodedString(str, key)); } /** * @Description: 解密 * @Param: [str] * @return: java.lang.String * @Author: 陈凯裕 * @Date: 2021/3/10 */ public static String decoded(String str) { String hexStr = null; try { hexStr = hexStrToStr(str); } catch (Exception e) { log.error("token解密失败"+e.getMessage()); } if (hexStr != null) { hexStr = encodedString(hexStr, key); } return hexStr; } /** * 转换 * @param str * @param password * @return */ private static String encodedString(String str, String password) { char[] pwd = password.toCharArray(); int pwdLen = pwd.length; char[] strArray = str.toCharArray(); for (int i=0; i