使用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


相关文章
|
8月前
|
Java
java密码学-数字签名
java密码学-数字签名
47 0
|
算法 Java 数据安全/隐私保护
数字签名的原理是什么?这篇文章给你答案(java代码实现)
数字签名在数据的交互中一直都占据着很重要的地位,因此,这篇文章对其原理进行整理总结一下。最后再给出代码的实现。
162 0
数字签名的原理是什么?这篇文章给你答案(java代码实现)
|
算法 Java API
RSA加密解密及数字签名Java实现
RSA算法是一种非对称密码算法,所谓非对称,就是指该算法需要一对密钥,使用其中一个加密,则需要用另一个才能解密。
527 0
|
Java 算法 数据安全/隐私保护
【Java小工匠聊密码学】--数字签名-RSA
1、RSA数字签名概述 使用RSA非对称加密技术实现的数字签名。 2、RSA数字签名算法分类 (1)MD (MD2withRSA、MD5withRSA) (2)SHA (SHA1withRSA、SHA256withRSA、SHA384withRSA、SHA512withRSA) 3、RSA数字签名实现 3.
1382 0
|
算法 Java 数据安全/隐私保护
【Java小工匠聊密码学】--数字签名-DSA
1、DSA数字签名概述 1.1 DSA加密算算法 DSA(Digital Signature Algorithm)是Schnorr和ElGamal签名算法的变种,被美国NIST作为数字签名标准(DigitalSignature Standard)。
1818 0
|
算法 Java
Java数字签名——ECDSA算法
  ECDSA   例如微软产品的序列号的验证算法。   Elliptic Curve Digital Signature Algorithm,椭圆曲线数字签名算法。   速度快,强度高,签名短 ——————————————————————————————————   密钥长度   112...
2802 0
|
算法 Java
Java数字签名——DSA算法
  RSA数字加密算法参考:http://www.cnblogs.com/LexMoon/p/javaRSA.html   DSS: 数字签名标准   DSA: 数字签名算法   DSA仅仅包含数字签名   ————————————————————————————————————   密钥...
1790 0
|
算法 Java 网络架构
Java数字签名——RSA算法
  数字签名:带有密钥(公钥,私钥)的消息摘要算法。        验证数据的完整性,认证数据的来源,抗否性   OSI参考模型   私钥签名,公钥验证   签名算法:RSA,DSA,ECDSA      算法1 :RSA       MD,SHA两类 ———————————————...
1899 0