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 ;}}