Digital Signature Algorithm--2. Dsa

Source: Internet
Author: User

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.Signature;
Import Java.security.interfaces.DSAPrivateKey;
Import Java.security.interfaces.DSAPublicKey;
Import Java.security.spec.PKCS8EncodedKeySpec;
Import Java.security.spec.X509EncodedKeySpec;


Import Com.sun.org.apache.xerces.internal.impl.dv.util.HexBin;


public class Imoocdsa {


private static String src = "IMOOC security DSA";
public static void Main (string[] args) {
JDKDSA ();
}
public static void Jdkdsa () {
try {
1. Initializing the key
Keypairgenerator keypairgenerator = keypairgenerator.getinstance ("DSA");
Keypairgenerator.initialize (512);
KeyPair KeyPair = Keypairgenerator.generatekeypair ();
Dsapublickey Dsapublickey = (dsapublickey) keypair.getpublic ();
Dsaprivatekey Dsaprivatekey = (dsaprivatekey) keypair.getprivate ();
2. Execute signature
Pkcs8encodedkeyspec Pkcs8encodedkeyspec = new Pkcs8encodedkeyspec (dsaprivatekey.getencoded ());
Keyfactory keyfactory = keyfactory.getinstance ("DSA");
Privatekey Privatekey = keyfactory.generateprivate (Pkcs8encodedkeyspec);
Signature Signature = signature.getinstance ("Sha1withdsa");
Signature.initsign (Privatekey);
Signature.update (Src.getbytes ());
Byte[] result = Signature.sign ();
System.out.println ("JDK DSA sign:" + hexbin.encode (result));
3. Verifying the signature

X509encodedkeyspec X509encodedkeyspec = new X509encodedkeyspec (dsapublickey.getencoded ());
Keyfactory = Keyfactory.getinstance ("DSA");
PublicKey publickey =keyfactory.generatepublic (X509ENCODEDKEYSPEC);
Signature=signature.getinstance ("Sha1withdsa");
Signature.initverify (PublicKey);
Signature.update (Src.getbytes ());
boolean bool = signature.verify (result);
System.out.println ("JDK DSA Verify:" + bool);
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}


}
}

Digital Signature Algorithm--2. Dsa

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.