使用java 进行数字签名
签名算法有:
MD5withRSA
SHA1withRSA
SHA256withRSA
代码如下:
package com.common.enu; /*** * 签名算法. * @author huangwei * @since 2013-10-28 */ public enum SignatureAlgorithm { SIGNATURE_ALGORITHM_MD5withRSA("MD5withRSA"), SIGNATURE_ALGORITHM_SHA1withRSA("SHA1withRSA"), SIGNATURE_ALGORITHM_SHA256withRSA("SHA256withRSA"); private final String value; //构造器默认也只能是private, 从而保证构造函数只能在内部使用 private SignatureAlgorithm(String value) { this.value = value; } public String getValue() { return value; } } /** * use private key sign * * @param message * data encrypted * @param key * @return * @throws Exception */ public static byte[] sign(String message, PrivateKey key, SignatureAlgorithm algorithm) throws Exception { return SystemUtil.sign(message.getBytes(SystemUtil.CHARSET_ISO88591), key, algorithm); } /** * use private key sign * * @param message * @param key * @return * @throws Exception */ public static byte[] sign(byte[] message, PrivateKey key, SignatureAlgorithm algorithm) throws Exception { Signature signetcheck = Signature.getInstance(algorithm.getValue()); signetcheck.initSign(key); signetcheck.update(message); return signetcheck.sign(); } /** * use public key verify sign * * @param message * @param signStr * @return * @throws Exception */ public static boolean verifySign(byte[] message, byte[] signBytes, PublicKey key, SignatureAlgorithm algorithm) throws Exception { if (message == null || signBytes == null || key == null) { return false; } Signature signetcheck = Signature.getInstance(algorithm.getValue()); signetcheck.initVerify(key); signetcheck.update(message); return signetcheck.verify(signBytes); } public static boolean verifySign(byte[] message, String signStr, PublicKey key, SignatureAlgorithm algorithm) throws Exception { byte[] signBytes = toBytes(signStr); return verifySign(message, signBytes, key, algorithm); } /*** * convert byte array to hex(16) bit string * * @param byte[] * @return hex(16) bit string */ public static String toHexString(byte[] b) { StringBuilder sb = new StringBuilder(b.length * 2); for (int i = 0; i < b.length; i++) { sb.append(HEXCHAR[(b[i] & 0xf0) >>> 4]); sb.append(HEXCHAR[b[i] & 0x0f]); } return sb.toString(); }
测试:
@Test public void test_sign() throws Exception { String message = "whuang3"; SignatureAlgorithm algorithm = SignatureAlgorithm.SIGNATURE_ALGORITHM_SHA256withRSA; //进行签名 byte[] signResult = SystemUtil.sign(message, privateKey, algorithm); System.out.println("sign result hex:" + SystemUtil.toHexString(signResult)); //校验签名 boolean isSuccess=SystemUtil.verifySign(message.getBytes(SystemUtil.CHARSET_ISO88591), signResult, publicKey, algorithm); System.out.println("sign1 :"+isSuccess); Assert.assertEquals(isSuccess, true); }
参考:http://security.group.iteye.com/group/wiki/2280-Non-symmetric-encryption-Digital-Signature
工具类com.common.util.SystemUtil 见附件
相关推荐
Java实现数字签名(ChinaSEI) 版权所有 郭克华 讲解详细,有实例,并且均可以测试通过!
java数字签名是指使用公钥加密技术,通过证书认证和签名验证来确保数据的安全和可靠性。数字签名的生成和验证过程 涉及到证书的申请、颁发和验证等多个方面。 在数字签名过程中,证书扮演着非常重要的角色。证书是...
java数字签名的讲解java数字签名的讲解java数字签名的讲解java数字签名的讲解
JAVA数字签名JAVA数字签名JAVA数字签名JAVA数字签名JAVA数字签名JAVA数字签名JAVA数字签名JAVA数字签名JAVA数字签名JAVA数字签名JAVA数字签名JAVA数字签名JAVA数字签名JAVA数字签名JAVA数字签名JAVA数字签名JAVA数字...
对Java Applet和Java Web Start进行数字签名
java xml数字签名工具类 拿来就能用
"java数字签名和证书应用" 数字签名是指使用某种签名算法对某些内容进行数字签名后得到的数字凭证。证书(Certificate)是数字签名的一种特殊形式,是用某种签名算法对某些内容(比如公钥)进行数字签名后得到的,...
java 国密算法实现,包含SM2 SM3 SM4和数字签名、数字证书的验证以及相应的说明文档
自己用JAVA写的数字签名程序,包括说明文档和截图。
数字签名 md5数字签名 c实现的md5数字签名 java实现的md5数字签名 数字签名 md5数字签名 c实现的md5数字签名 java实现的md5数字签名
用Java数字签名提供XML安全.doc
java数字签名,可以解决数字签名的问题。
Java 实现数字签名的简单例子
此项目基于java标准数字签名验签技术可直接二次开发
java 相关程序 加签验签 数字签名 Xml报文形式
该源码分为两个包,一个rsa包,一个...SignatureData.java 实现数字签名 VerifySignature.java 实现数字签名验证,并含有主程序(main函数) cn.chd.david.utils BitByte.java 工具类,实现二进制流和十六进制流转换
压缩包里面word文件说明了使用JavaApplet时客户端运行jar包的生成方法及如何数字签名,JavaAutoPlug.exe是帮助我们完成打包和签名的一个工具,需安装Framework2.0。
java 加密 数字签名java 加密 数字签名java 加密 数字签名java 加密 数字签名