import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.HexUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.crypto.asymmetric.KeyType; import cn.hutool.crypto.asymmetric.RSA; import com.wafer.global.Const; import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;

/**

  • @author tianjian
  • @ClassName: RSA2048Util
  • @Description: 加密解密算法 */ @Slf4j public class RSA2048Util { private static RSA rsa = new RSA(Const.RSA_PRI_KEY, Const.RSA_PUB_KEY);

private RSA2048Util() { }

/**

  • @Author 田剑
  • @Description //TODO RSA-2048 解密
  • @Date 2020/10/10 15:20
  • @Param [encodeStr]
  • @return java.lang.String / public static String RsaDecode(String encodeStr) { if(StrUtil.isEmptyOrUndefined(encodeStr)){ return ""; } byte[] decrypt = rsa.decrypt(encodeStr, KeyType.PrivateKey); String result= ""; try { result = new String(decrypt,"UTF-8"); System.out.println(result); } catch (UnsupportedEncodingException e) { log.info("解密异常",e); } return result; } /*
  • @Author 田剑
  • @Description //TODO RSA-2048 加密
  • @Date 2020/10/10 15:20
  • @Param [str]
  • @return java.lang.String */ public static String RsaEncode(String str) { //公钥加密,私钥解密 if(StrUtil.isEmptyOrUndefined(str)){ return ""; } String result=""; byte[] encrypt = rsa.encrypt(StrUtil.bytes(str, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey); result = HexUtil.encodeHexStr(encrypt); System.out.println(result.length()); return result; } // // public static void main(String[] args) { // // String str= "天王盖地虎,宝塔镇河妖"; // String a = ""; //// a = RsaEncode(str); // a="01168710c4ac9dc835ab0f7af2bcf07bc20769843d7ed526fa6d7f8571bda4741feede9cbfa4e365ea686678d92e550c54057ade9a4c4b0fd421399368854d43"; // RsaDecode(a); //// System.out.println(Const.RSA_PRI_KEY); //// System.out.println(Const.RSA_PUB_KEY); // // }

}