Java implementation of RSA encryption algorithm

Source: Internet
Author: User

 packagersa;ImportJava.security.*;Importjava.security.interfaces.*;ImportJavax.crypto.*; public classTest { public Static voidmain (string[] Args) {Try{String Message= "yuexiu district, Guangzhou, Guangdong province"; Test P=NewTest (); System.out.println ("clear text is" +message); //generating public and private key pairs, generating objects based on the RSA algorithmKeypairgenerator Keypairgen = keypairgenerator.getinstance ("RSA"); //initializes the key pair generator with a key size of 1024 bitsKeypairgen.initialize (1024); //generate a key pair, saved in KeyPairKeyPair KeyPair =Keypairgen.generatekeypair (); //get the private key and public keyRsaprivatekey Privatekey =(rsaprivatekey) keypair.getprivate (); Rsapublickey PublicKey=(rsapublickey) keypair.getpublic (); //encrypt with public key            byte[] srcbytes =message.getbytes (); byte[] resultbytes =Test.encrypt (publickey, srcbytes); String result=NewString (resultbytes); System.out.println ("ciphertext after encrypting with public key is:" +result); //decrypt with private key            byte[] decbytes =Test.decrypt (privatekey,resultbytes); String Dec=NewString (decbytes); System.out.println ("the result of encrypting with the private key is:" +dec); }Catch(Exception E) {e.printstacktrace (); }    }        protected Static byte[] Encrypt (rsapublickey publickey,byte[] srcbytes) {        if(publickey! =NULL){            Try{                //Cipher is responsible for completing encryption or decryption work, based on RSACipher Cipher = cipher.getinstance ("RSA"); //Initializes a cipher object based on the public keyCipher.init (cipher.encrypt_mode, publickey); //encrypted, The result is saved into resultbytes and returned                byte[] resultbytes =cipher.dofinal (srcbytes); returnresultbytes; }Catch(Exception E) {e.printstacktrace (); }        }        return NULL; }        protected Static byte[] Decrypt (rsaprivatekey privatekey,byte[] encbytes) {        if(privatekey! =NULL){            Try{Cipher Cipher= Cipher.getinstance ("RSA"); //Initializes a cipher object based on the private keyCipher.init (cipher.decrypt_mode, privatekey); //decrypt and save the results into Resultbytes                byte[] decbytes =cipher.dofinal (encbytes); returndecbytes; }Catch(Exception E) {e.printstacktrace (); }        }        return NULL; }}

Java implementation of RSA encryption algorithm

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.