zoukankan      html  css  js  c++  java
  • 数字签名算法--3.ECDSA

    package Imooc;


    import java.security.KeyFactory;
    import java.security.KeyPair;
    import java.security.KeyPairGenerator;
    import java.security.NoSuchAlgorithmException;
    import java.security.PrivateKey;
    import java.security.PublicKey;
    import java.security.interfaces.ECPrivateKey;
    import java.security.interfaces.ECPublicKey;
    import java.security.spec.PKCS8EncodedKeySpec;
    import java.security.spec.X509EncodedKeySpec;
    import java.security.Signature;


    import com.sun.org.apache.xerces.internal.impl.dv.util.HexBin;
    //import com.sun.org.apache.bcel.internal.classfile.Signature;


    public class ImoocECDSA {


    private static String src = "imooc security ecdsa";
    public static void main(String[] args) {
    jdkECDSA();
    }
    //java7以后使用
    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 ecdsa sign:"+HexBin.encode(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 verify:"+bool);
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }
    }
  • 相关阅读:
    Java-01,计算1-10的阶乘之和
    软件工程研究生面试机试考题-2018
    nginx会话保持之sticky模块
    Dubbo原理简介、与Zookeeper整合利用
    Day41 openstack基础
    krb5-libs这个RPM包删掉了导致ssh无法连接
    day40 数据结构-算法(二)
    进程上下文频繁切换导致load average过高
    day39 算法基础
    应用性能管理工具PinPoint介绍
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/7080715.html
Copyright © 2011-2022 走看看