Smile_0020
2015-08-27 15:46
我运行出来报错,
java.security.NoSuchAlgorithmException: EC KeyPairGenerator not available
at java.security.KeyPairGenerator.getInstance(KeyPairGenerator.java:176)
at ECDSA.ECDSA.jdkecdsa(ECDSA.java:26)
at ECDSA.ECDSA.main(ECDSA.java:20)
怎么回事????哪错了
JDK的版本可能低,不支持
我的代码
package ECDSA; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; 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.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import org.apache.commons.codec.binary.Hex; public class ECDSA { 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 ecPrivateKey =(ECPrivateKey) keyPair.getPrivate(); //执行签名 PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(ecPrivateKey.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 escdsa sign:"+Hex.encodeHexString(result)); //验证签名 X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(ecPublicKey.getEncoded()); keyFactory = KeyFactory.getInstance("EC"); PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec); signature = Signature.getInstance("SHA1withECDSA"); signature.initVerify(publicKey); signature.update(src.getBytes()); boolean bool = signature.verify(result); System.out.println("jdk ecdsa verfiy:"+bool); } catch (Exception e) { e.printStackTrace(); } } }
Java实现数字签名
39190 学习 · 22 问题
相似问题