社会主义小青年的博客

噫吁嚱,危呼高哉!Talk___is___Cheap ,show___ me___ your___ code... >
客官,请打赏

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.

 

posted @ 2018-04-09 11:38  小青年先生  阅读(300)  评论(0编辑  收藏  举报