import java.nio.charset.StandardCharsets; import java.security.KeyFactory; import java.security.PrivateKey; import java.security.Signature; import java.security.spec.PKCS8EncodedKeySpec; import java.util.Base64; import org.apache.commons.codec.digest.DigestUtils; String stringSign = "需要加密的信息"; String pubencrypt_key = "私钥信息"; //进行加密并且进行2次base64转码 public static String sign(String content, String privateKey) { try{ PKCS8EncodedKeySpec pkcs8 = new PKCS8EncodedKeySpec(Base64.getMimeDecoder().decode(privateKey.getBytes(StandardCharsets.UTF_8))); KeyFactory kf = KeyFactory.getInstance("RSA"); PrivateKey pk = kf.generatePrivate(pkcs8); Signature sha1WithRSA = Signature.getInstance("SHA1WithRSA"); sha1WithRSA.initSign(pk); sha1WithRSA.update(content.getBytes(StandardCharsets.UTF_8)); String sign = Base64.getEncoder().encodeToString(sha1WithRSA.sign()); return Base64.getEncoder().encodeToString(sign.getBytes()); }catch(Exception e){ e.printStackTrace(); } return null; } String sign = sign(aeskey,pubencrypt_key); vars.put("sign",sign); //log.info("sign = " + sign);