Digital signature using DSA Algorithm

Source: Internet
Author: User
Tags sendmsg

The following is an example of digital signature using the DSA Signature Algorithm in Java.

Digital signature algorithm (DSA) is a variant of Schnorr and Elgamal signature algorithms. It is used by NIST as the digital signature standard ). Refer to the description of signature in the JDK document: the NIST Standard DSA algorithm should include the hash algorithm SHA-1 and the encryption algorithm DSA.

Signature. getinstance ("DSA") is equivalent to signature. getinstance ("sha1withdsa"); if you use DSA as the digital signature encryption algorithm, you can only use sha1 as the message digest algorithm. If you use RSA as the digital signature encryption algorithm, there are multiple options for the message digest algorithm. Therefore, you can specify the signature algorithm as md2withrsa, md5withrsa, or sha1withrsa. For more information about how to use RSA to implement digital signatures, see 《Digital signature using RSAIf you are not familiar with digital signatures, read 《Introduction to digital signaturesAnd the Java digital signature and digital certificate dsa_demo.java File

Import Java. security. invalidkeyexception; 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. signatureexception; public class dsa_demo {dsa_demo () {Init ();} private void Init () {prepare (); dosenderwork (); doreceiverwork ();} // share By sender and receiversignature sign; // belong to sender, it visible to sender and receiverpublickey publickey; // belong to sender, it is only visible to senderprivatekey privatekey; private void prepare () {keypairgenerator keygen = NULL; try {// instantiate the public key/private key pair generator keygen = keypairgenerator of a dsa algorithm. getinstance ("DSA");} catch (nosuchalgorithmexception e) {// todo auto-generated catch blocke. printstacktrace ();} int key Size = 1024; // set the length of the public key/private key pair keygen. initialize (keysize); // generate a public key of the RSA algorithm/private key keypair = keygen. generatekeypair (); privatekey = keypair. getprivate (); publickey = keypair. getpublic (); try {// instantiate the signature of a DSA Signature Algorithm. sign = signature. getinstance ("DSA");} catch (nosuchalgorithmexception e) {// todo auto-generated catch blocke. printstacktrace () ;}} void dosenderwork () {string words = "This is Robin. how are you? This a DSA Signature Demo "; message MSG = new message (words. getbytes (); try {// set the private key sign for encryption hash code. initsign (privatekey);} catch (invalidkeyexception e) {// todo auto-generated catch blocke. printstacktrace ();}
Try {// set the input sign of the hash algorithm. update (MSG. getbody ();} catch (signatureexception e) {// todo auto-generated catch blocke. printstacktrace ();} byte data [] = NULL; try {// hash, encrypt the generated hash code, and return data = sign. sign ();} catch (signatureexception e) {// todo auto-generated catch blocke. printstacktrace ();} // Add the encrypted Hash (Signature) to the Message MSG. setsignature (data); // send the message sendmsg (MSG);} message sendingmsg; void sendmsg (Message sendmsg) {sendingmsg = Sen Dmsg; system. out. println ("Sending Message signed by DSA");} void doreceiverwork () {// receive message MSG = getreceivedmsg (); try {// set the public key used to decrypt the hash code. Sign. initverify (publickey);} catch (invalidkeyexception E1) {// todo auto-generated catch blocke1.printstacktrace ();} Try {// set the input sign of the hash algorithm. update (MSG. getbody ();} catch (signatureexception e) {// todo auto-generated catch blocke. printstacktrace ();} Try {/* for hash calculation, and compare whether the calculated hash code is consistent with the decrypted hash code. * If they are consistent, the verification succeeds. Otherwise, the verification fails. */If (sign. Verify (msg. getsignature () {system. Out. println ("the digital signature is successfully verified! ");} Else {system. Out. println (" Digital Signature Verification Failed! ") ;}} Catch (signatureexception e) {// todo auto-generated catch blocke. printstacktrace () ;}} message getreceivedmsg () {system. out. println ("processing ing message"); Return sendingmsg ;}} class message {private byte [] body; private byte [] signature; message (byte data []) {body = data;} byte [] getbody () {return body;} byte [] getsignature () {return signature;} void setsignature (byte data []) {Signature = data ;}}

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.