使用java 进行数字签名

使用Java 进行数字签名

签名算法有:

MD5withRSA

SHA1withRSA

SHA256withRSA

代码如下:

Java代码   收藏代码
  1. package com.common.enu;  
  2. /*** 
  3.  * 签名算法. 
  4.  * @author huangwei 
  5.  * @since 2013-10-28 
  6.  */  
  7. public enum SignatureAlgorithm {  
  8.     SIGNATURE_ALGORITHM_MD5withRSA("MD5withRSA"),  
  9.     SIGNATURE_ALGORITHM_SHA1withRSA("SHA1withRSA"),  
  10.     SIGNATURE_ALGORITHM_SHA256withRSA("SHA256withRSA");  
  11.       
  12.     private final String value;  
  13.   
  14.     //构造器默认也只能是private, 从而保证构造函数只能在内部使用  
  15.     private SignatureAlgorithm(String value) {  
  16.         this.value = value;  
  17.     }  
  18.       
  19.     public String getValue() {  
  20.         return value;  
  21.     }  
  22. }  
  23.   
  24. /** 
  25.      * use private key sign 
  26.      *  
  27.      * @param message 
  28.      *            data encrypted 
  29.      * @param key 
  30.      * @return 
  31.      * @throws Exception 
  32.      */  
  33.     public static byte[] sign(String message, PrivateKey key,  
  34.             SignatureAlgorithm algorithm) throws Exception {  
  35.         return SystemUtil.sign(message.getBytes(SystemUtil.CHARSET_ISO88591),  
  36.                 key, algorithm);  
  37.     }  
  38.   
  39. /** 
  40.      * use private key sign  
  41.      *  
  42.      * @param message 
  43.      * @param key 
  44.      * @return 
  45.      * @throws Exception 
  46.      */  
  47.     public static byte[] sign(byte[] message, PrivateKey key,  
  48.             SignatureAlgorithm algorithm) throws Exception {  
  49.         Signature signetcheck = Signature.getInstance(algorithm.getValue());  
  50.         signetcheck.initSign(key);  
  51.         signetcheck.update(message);  
  52.         return signetcheck.sign();  
  53.     }  
  54. /** 
  55.      * use public key verify sign 
  56.      *  
  57.      * @param message 
  58.      * @param signStr 
  59.      * @return 
  60.      * @throws Exception 
  61.      */  
  62.     public static boolean verifySign(byte[] message, byte[] signBytes,  
  63.             PublicKey key, SignatureAlgorithm algorithm) throws Exception {  
  64.         if (message == null || signBytes == null || key == null) {  
  65.             return false;  
  66.         }  
  67.         Signature signetcheck = Signature.getInstance(algorithm.getValue());  
  68.         signetcheck.initVerify(key);  
  69.         signetcheck.update(message);  
  70.         return signetcheck.verify(signBytes);  
  71.     }  
  72. public static boolean verifySign(byte[] message, String signStr,  
  73.             PublicKey key, SignatureAlgorithm algorithm) throws Exception {  
  74.         byte[] signBytes = toBytes(signStr);  
  75.         return verifySign(message, signBytes, key, algorithm);  
  76.     }  
  77. /*** 
  78.      * convert byte array to hex(16) bit string 
  79.      *  
  80.      * @param byte[] 
  81.      * @return hex(16) bit string 
  82.      */  
  83.     public static String toHexString(byte[] b) {  
  84.         StringBuilder sb = new StringBuilder(b.length * 2);  
  85.         for (int i = 0; i < b.length; i++) {  
  86.             sb.append(HEXCHAR[(b[i] & 0xf0) >>> 4]);  
  87.             sb.append(HEXCHAR[b[i] & 0x0f]);  
  88.         }  
  89.         return sb.toString();  
  90.     }  

 测试:

Java代码   收藏代码
  1. @Test  
  2.     public void test_sign() throws Exception {  
  3.         String message = "whuang3";  
  4.         SignatureAlgorithm algorithm = SignatureAlgorithm.SIGNATURE_ALGORITHM_SHA256withRSA;  
  5. //进行签名  
  6.         byte[] signResult = SystemUtil.sign(message, privateKey, algorithm);  
  7.         System.out.println("sign result hex:" + SystemUtil.toHexString(signResult));  
  8. //校验签名  
  9.         boolean isSuccess=SystemUtil.verifySign(message.getBytes(SystemUtil.CHARSET_ISO88591),  
  10.                 signResult, publicKey, algorithm);  
  11.         System.out.println("sign1 :"+isSuccess);  
  12.         Assert.assertEquals(isSuccess, true);  
  13.     }  

 参考:http://security.group.iteye.com/group/wiki/2280-Non-symmetric-encryption-Digital-Signature


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值