RSA 数字签名算法(Java版)
数字签名算法Java版 还是上代码吧
public static final String SIGN_ALGORITHMS = "SHA1WithRSA"; /** * RSA签名 * @param content 待签名数据 * @param privateKey 私钥 * @param input_charset 编码格式 * @return 签名值 */ public static String sign(String content, String privateKey, String input_charset) { try { byte[] decode =Base64.getDecoder().decode(privateKey); PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(decode); KeyFactory keyf= KeyFactory.getInstance("RSA"); PrivateKey priKey= keyf.generatePrivate(priPKCS8); java.security.Signature signature = java.security.Signature.getInstance(SIGN_ALGORITHMS); signature.initSign(priKey); signature.update(content.getBytes(input_charset) ); byte[] signed = signature.sign(); return Base64.getEncoder().encodeToString(signed); } catch (Exception e) { e.printStackTrace(); } return null; }
调用方式:
String sign = sign(”content”,”privateKey”,"utf-8");
Over,Tks.