求大神看一下,我的代码运行报错

来源:4-1 数字签名算法ECDSA

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)

怎么回事????哪错了

写回答 关注

2回答

  • imooc_幕课网
    2016-07-23 17:01:53

    JDK的版本可能低,不支持

    京飞 回复京飞

    jdk1.7之后的才行

    2017-04-26 15:03:34

    共 2 条回复 >

  • Smile_0020
    2015-08-27 15:47:14

    我的代码

    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实现数字签名

介绍Java实现数字签名,有效保障网络传输过程中的数据安全

39190 学习 · 22 问题

查看课程

相似问题