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 later use
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 ();
}
}
}
Digital Signature Algorithm--3. ECDSA