Java Public/Private Key Algorithm

Source: Internet
Author: User
Today, I saw a Java Public/Private Key algorithm on the Internet. Very good...
Add the information public final byte [] Sign () throws signatureexception to return the signature array, provided that initsign and updatepublic final void initverify (publickey) throws invalidkeyexception uses the specified public key initialization parameter: Public final Boolean verify (byte [] signature) When publickey is used for verification throws signatureexception to verify whether the signature is valid, provided that the parameter has been initverify initialized: signature signature array */import Java. security. *; import Java. security. spec. *; public class testdsa {public static void main (string [] ARGs) throws Java. security. nosuchalgorithmexception, Java. lang. exception {testdsa my = new testdsa (); my. run ();} public void run () {// digital signature to generate a key pair // The first step is to generate a key pair. If the key pair has been generated, skip this process, myprikey. dat needs to be saved locally // while mypubkey. dat to publish to other users if (New Java. io. file ("myprikey. dat ")). exists () = false) {If (generatekey () = false) {system. out. println ("failed to generate key pair"); Return ;}/// step 2, this user // reads the private key from the file, sign a string and save it in a file (myinfo. in dat) // and then put myinfo. dat is sent out // For convenience, the digital signature is also put in myifno. in the DAT file, you can also send try {Java. io. objectinputstream in = new Java. io. objectinputstream (New Java. io. fileinputstream ("myprikey. dat "); privatekey myprikey = (privatekey) in. readobject (); In. close (); // Java. security. spec. x509encodedkeyspec pubx509 = new Java. security. spec. x509encodedkeyspec (bx509); // Java. security. spec. x509encodedkeyspec pubkeyencode = Java. security. spec. x509encodedkeyspec string myinfo = "this is my information"; // The information to be signed // generate a digital signature using the private key pair information Java. security. signature signet = Java. security. signature. getinstance ("DSA"); signet. initsign (myprikey); signet. update (myinfo. getbytes (); byte [] signed = signet. sign (); // digital signature of information system. out. println ("signed (Signature content) =" + byte2hex (Signed); // Save the information and digital signature in a file. java. io. objectoutputstream out = new Java. io. objectoutputstream (New Java. io. fileoutputstream ("myinfo. dat "); out. writeobject (myinfo); out. writeobject (Signed); out. close (); system. out. println ("signature and file generation succeeded");} catch (Java. lang. exception e) {e. printstacktrace (); system. out. println ("failed to sign and generate a file") ;}; // step 3 // others obtain the public key and file of the user in public mode. // others use the public key of the user, check the file. If the file is successfully checked, it indicates the information published by this user. // try {Java. io. objectinputstream in = new Java. io. objectinputstream (New Java. io. fileinputstream ("mypubkey. dat "); publickey pubkey = (publickey) in. readobject (); In. close (); system. out. println (pubkey. getformat (); In = new Java. io. objectinputstream (New Java. io. fileinputstream ("myinfo. dat "); string info = (string) in. readobject (); byte [] signed = (byte []) in. readobject (); In. close (); Java. security. signature signetcheck = Java. security. signature. getinstance ("DSA"); signetcheck. initverify (pubkey); signetcheck. update (info. getbytes (); If (signetcheck. verify (Signed) {system. out. println ("info =" + info); system. out. println ("normal signature");} else system. out. println ("non-signature normal");} catch (Java. lang. exception e) {e. printstacktrace () ;};}// generate a pair of file myprikey. dat and mypubkey. dat --- private key and public key. // the public key must be sent by the user (file, network, and other methods) to other users. The private key is stored in the local public Boolean generatekey () {try {Java. security. keypairgenerator keygen = Java. security. keypairgenerator. getinstance ("DSA"); // securerandom secrand = new securerandom (); // secrand. setseed ("tttt ". getbytes (); // initialize the random generator // keygen. initialize (576, secrand); // initialize the key generator keygen. initialize (512); keypair keys = keygen. genkeypair (); // keypair keys = keygen. generatekeypair (); // generate the key group publickey pubkey = keys. getpublic (); privatekey prikey = keys. getprivate (); Java. io. objectoutputstream out = new Java. io. objectoutputstream (New Java. io. fileoutputstream ("myprikey. dat "); out. writeobject (prikey); out. close (); system. out. println ("Write object prikeys OK"); out = new Java. io. objectoutputstream (New Java. io. fileoutputstream ("mypubkey. dat "); out. writeobject (pubkey); out. close (); system. out. println ("Write object pubkeys OK"); system. out. println ("key pair generated successfully"); Return true;} catch (Java. lang. exception e) {e. printstacktrace (); system. out. println ("failed to generate the key pair"); Return false ;};} Public String byte2hex (byte [] B) {string HS = ""; string stmp = ""; for (INT n = 0; n
 

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.