慕用1861190
2016-01-26 18:39
package imoocECDA;
import java.awt.HeadlessException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.KeyStore.PrivateKeyEntry;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.security.spec.ECPrivateKeySpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
public class ImoocECDSA {
private static String src="imooc security ecdsa";
public static void main(String[] args) {
jdkECDSA();
}
public static void jdkECDSA(){
try {
//初始化密钥
KeyPairGenerator keyPairGenerator=KeyPairGenerator.getInstance("EC");
keyPairGenerator.initialize(256);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
ECPublicKey ecPublicKey=(ECPublicKey)keyPair.getPublic();
ECPrivateKey ecpPublicKey=(ECPrivateKey)keyPair.getPrivate();
//执行签名
PKCS8EncodedKeySpec pkcs8EncodedKeySpec=new PKCS8EncodedKeySpec(ecpPublicKey.getEncoded());
KeyFactory keyFactory=KeyFactory.getInstance("EC");
PrivateKey privateKey=keyFactory.generatePrivate(pkcs8EncodedKeySpec);
Signature signature=Signature.getInstance("SHA1withECDSA");
signature.initSign(privateKey);
signature.update(src.getBytes());
byte[] result=signature.sign();
System.out.println("jdk ecdsa sign:"+Hex.encodeHexString(result));
//验证
X509EncodedKeySpec x509EncodedKeySpec=new X509EncodedKeySpec(ecpPublicKey.getEncoded());
keyFactory=KeyFactory.getInstance("EC");
PublicKey publicKey=keyFactory.generatePublic(x509EncodedKeySpec);
signature.initVerify(publicKey);
signature.update(src.getBytes());
boolean bool=signature.verify(result);
System.out.println("jdk ecdsa verify:"+bool);
} catch (Exception e) {
e.printStackTrace();
}
}
}
黑体那一句为什么总是报错?谢谢回答
留个记号 啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾啾
System.out.println("jdk ecdsa sign:"+Hex.encodeHexString(result));
Java实现数字签名
39190 学习 · 22 问题
相似问题