电子签名用Java怎么实现
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了电子签名用Java怎么实现相关的知识,希望对你有一定的参考价值。
电子签名用Java怎么实现
参考技术A import java.security.InvalidKeyException;import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;
import java.security.SignatureException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* ECDSA 160bit 签名及签名验证例子
*/
public class ECDSASignature
public static void main(String argv[])
signatureTest();
public static void signatureTest()
/**
* 密钥对生成
*/
KeyPairGenerator keyPairGenerator = null;
try
keyPairGenerator = KeyPairGenerator.getInstance("EC");
catch (NoSuchAlgorithmException ex)
return;
SecureRandom secureRandom = new SecureRandom();
keyPairGenerator.initialize(160, secureRandom);
KeyPair pair = keyPairGenerator.generateKeyPair();
Key publicKey = pair.getPublic();
Key privateKey = pair.getPrivate();
// 字符串
String hako = "test";
/**
* 私钥签名(ECDSA 160bit)
*/
byte[] sign = null;
try
Signature signatureSign = null;
signatureSign = Signature.getInstance("NONEwithECDSA");
signatureSign.initSign((PrivateKey) privateKey, secureRandom);
signatureSign.update(hako.getBytes());
sign = signatureSign.sign();
System.out.println("sign: " + new String(sign));
catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException ex)
Logger.getLogger(ECDSASignature.class.getName()).log(Level.SEVERE, null, ex);
/**
* 用公钥进行签名验证
*/
Signature signatureVerify = null;
try
signatureVerify = Signature.getInstance("NONEwithECDSA");
signatureVerify.initVerify((PublicKey) publicKey);
signatureVerify.update(hako.getBytes());
boolean verifyResult = signatureVerify.verify(sign);
System.out.println(verifyResult ? "签名OK" : "签名NG");
catch (NoSuchAlgorithmException | InvalidKeyException | SignatureException e)
Logger.getLogger(ECDSASignature.class.getName()).log(Level.SEVERE, null, e);
参考技术B 需要买第三方控件吧!http://www.huuinn.com/java-%E7%94%B5%E5%AD%90%E7%AD%BE%E5%90%8D%E8%8C%83%E4%BE%8B%E4%BB%A3%E7%A0%81/
以上是关于电子签名用Java怎么实现的主要内容,如果未能解决你的问题,请参考以下文章