Implementation of RSA digital signature with JDK method

Source: Internet
Author: User

JDK only supports Md2withrsa, Md5withrsa, Sha1withrsa

Other such as Sha512withrsa require third-party package support, such as BC (bouncy castle)

The key length of the JDK is still 1024 by default

1  PackageJdbc.pro.lin;2 3 Importjava.security.InvalidKeyException;4 Importjava.security.NoSuchAlgorithmException;5 ImportJava.security.PrivateKey;6 ImportJava.security.PublicKey;7 Importjava.security.Signature;8 Importjava.security.SignatureException;9 ImportJava.util.Map;Ten  One ImportJdbc.pro.lin.MyRSA; A  - /** - * RSA Digital signature, borrow the algorithm in Myrsa, no longer repeat the * The digital signature follows the "private key signature, public key verification" principle, because the private key is a personal authentication -  * @authorKinsley -  * -  */ +  Public classMysignature { -      +     /**Digital Signature Algorithm. JDK only provides Md2withrsa, Md5withrsa, Sha1withrsa, and other algorithms require third-party packages to support*/ A      Public Static FinalString signature_algorithm = "Sha1withrsa"; at      -      Public Static FinalString Plain_text = "ManUtd is the greatest club"; -      Public Static voidMain (string[] args) -     { -         //set up two sets of public key pairs -Map<string,byte[]> KeyMap1 =myrsa.generatekeybytes (); inPublicKey publicKey1 =Myrsa.restorepublickey (Keymap1.get (Myrsa.public_key)); -Privatekey PrivateKey1 =Myrsa.restoreprivatekey (Keymap1.get (Myrsa.private_key)); to          +Map<string,byte[]> KEYMAP2 =myrsa.generatekeybytes (); -PublicKey PublicKey2 =Myrsa.restorepublickey (Keymap2.get (Myrsa.public_key)); thePrivatekey PrivateKey2 =Myrsa.restoreprivatekey (Keymap2.get (Myrsa.private_key)); *          $         /**Suppose you now send a message to B after a signaturePanax Notoginseng * A is encrypted with the public key of B - * Sign with your private key of a the          */ +         byte[] Encodedtext =Myrsa.rsaencode (PublicKey2, Plain_text.getbytes ()); A         byte[] Signature =Sign (PrivateKey1, Plain_text.getbytes ()); the          +         /** - * Now B has received a message, take two steps $ * Decrypted with the private key of B to get plaintext $ * Check the plaintext and a's public key for the verification operation -          */ -          the         byte[] Decodedtext =Myrsa.rsadecode (PrivateKey2, Encodedtext). GetBytes (); -System.out.println ("Decoded Text:" +NewString (Decodedtext));Wuyi          theSystem.out.println ("Signature is" +Verify (PublicKey1, signature, decodedtext)); -     } Wu      -     /** About * Signature, three-step walk $ * 1. Instantiation, incoming algorithm - * 2. Initialize, incoming private key - * 3. Signature -      * @paramKey A      * @paramplaintext +      * @return the      */ -      Public Static byte[] Sign (Privatekey Privatekey,byte[] plaintext) $     { the         Try { the             //instantiation of theSignature Signature =signature.getinstance (signature_algorithm); the              -             //Initialize, incoming private key in signature.initsign (privatekey); the              the             //Update About signature.update (plaintext); the              the             //Signature the             returnsignature.sign (); +              -}Catch(NoSuchAlgorithmException | InvalidKeyException |signatureexception e) { the             //TODO auto-generated Catch blockBayi e.printstacktrace (); the         } the          -         return NULL; -     } the      the     /** the * Verification, three-step walk the * 1. Instantiation, incoming algorithm - * 2. Initialize, incoming public key the * 3. Verification the      * @paramPublicKey the      * @paramsignatureverify94      * @paramplaintext the      * @return the      */ the      Public Static BooleanVerify (PublicKey publickey,byte[] signatureverify,byte[] plaintext)98     { About         Try { -             //instantiation of101Signature Signature =signature.getinstance (signature_algorithm);102             103             //Initialize104 signature.initverify (publickey); the             106             //Update107 signature.update (plaintext);108             109             //Verification the             returnsignature.verify (signatureverify);111}Catch(NoSuchAlgorithmException | InvalidKeyException |signatureexception e) { the             //TODO auto-generated Catch block113 e.printstacktrace (); the         } the          the         return false;117     }118}

Implementation of RSA digital signature with JDK method

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.