Java Digital Signature

Source: Internet
Author: User
Tags asymmetric encryption

 

The digital signature is based on asymmetric encryption between the public key and the private key. The sender uses the private key to encrypt the message digest (signature). The receiver uses the public key to decrypt the message digest to verify whether the signature is owned by someone.

Basic steps:

Obtain the keypairgenerator Instance Object and call its generatekeypair () method to create the keypair object.

Call the getprivate and getpublic methods of the keypair object to obtain the privatekey object and the publickey object respectively.

Obtain the signature instance object, call its initsign () method and specify the privatekey object, and then call the update and Sign methods to generate a signature.

Call the initverify () method of the signature object and specify the publickey object, and then call the update and verify () methods to verify the signature of the original data.

 

Example:

 

Package COM. study. security; import Java. io. bytearrayoutputstream; import Java. io. fileinputstream; import Java. io. fileoutputstream; import Java. io. objectinputstream; import Java. io. objectoutputstream; import Java. security. key; import Java. security. keypair; import Java. security. keypairgenerator; import Java. security. privatekey; import Java. security. publickey; import Java. security. signature;/*** digital signature * Private Key signature public key verification * @ Author administrator **/public class digitsign {public static void main (string [] ARGs) throws exception {sign (); Verify ();} // signature Private Static void sign () throws exception {keypairgenerator generator = keypairgenerator. getinstance ("RSA"); keypair = generator. generatekeypair (); publickey = keypair. getpublic (); privatekey = keypair. getprivate (); signature Signature = signature. getinst Ance ("md5withrsa"); signature. initsign (privatekey); signature. Update ("Hello Java! ". Getbytes ("UTF-8"); byte [] signedresult = signature. sign (); // save data and Public Key savekey (publickey, "sign_public.key"); savedata (signedresult, "sign_data.data");} // verify Private Static void verify () throws exception {byte [] DATA = readdata ("sign_data.data"); publickey = (publickey) readkey ("sign_public.key"); signature Signature = signature. getinstance ("md5withrsa"); signature. initverify (publickey); signature. update ("He LLO Java! ". Getbytes ("UTF-8"); Boolean isyoursign = signature. verify (data); system. out. println ("Verification Result:" + isyoursign);} // Method for saving data public static void savedata (byte [] Results, string dataname) throws exception {fileoutputstream fosdata = new fileoutputstream (dataname); fosdata. write (results); fosdata. close () ;}// method for data recovery public static byte [] readdata (string dataname) throws exception {fileinputstream fisdat = new fileinputstream (Dataname); // read binary data bytearrayoutputstream arrayoutputstream = new bytearrayoutputstream (); int Len = 0; byte [] DATA = new byte [1024]; while (LEN = fisdat. read (data ))! =-1) {arrayoutputstream. write (data, 0, Len);} byte [] result = arrayoutputstream. tobytearray (); arrayoutputstream. close (); fisdat. close (); return result;} // Method for saving the key public static void savekey (Key key, string keyname) throws exception {fileoutputstream foskey = new fileoutputstream (keyname ); objectoutputstream OOS = new objectoutputstream (foskey); OOS. writeobject (key); OOS. close (); foskey. close () ;}// Method for restoring the key public static key readkey (string keyname) throws exception {fileinputstream fiskey = new fileinputstream (keyname); objectinputstream oiskey = new objectinputstream (fiskey ); key key = (key) oiskey. readobject (); oiskey. close (); fiskey. close (); Return key ;}}

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.