Asymmetric encryption algorithm of information encryption RSA

Source: Internet
Author: User
Tags asymmetric encryption

Before everyone has summed up, based on the key exchange DH algorithm, now for you to introduce a factor decomposition based RSA algorithm, this encryption algorithm has two implementation forms: 1, public key encryption, private key decryption, 2, private key encryption, public key decryption. The following for you to analyze the implementation of code, compared to the DH algorithm, RSA seems a little simple.
Initialize key:

Keypairgenerator keypairgenerator = keypairgenerator.getinstance ("RSA");            Keypairgenerator.initialize (+);             = Keypairgenerator.generatekeypair ();             = (Rsaprivatekey) keypair.getprivate (); // private key            Rsapublickey Rsapublickey = (rsapublickey) keypair.getpublic (); // Public Key            System.out.println ("Privatekey:" +base64.encodebase64string (rsaprivatekey.getencoded ()));            System.out.println ("PublicKey:" +base64.encodebase64string (rsapublickey.getencoded ()));

1, private key encryption, public key decryption:

//private key encryption, public key decryption--encryptionPkcs8encodedkeyspec Pkcs8encodedkeyspec =NewPkcs8encodedkeyspec (rsaprivatekey.getencoded ()); Keyfactory privatekeyfactory= Keyfactory.getinstance ("RSA"); Privatekey Privatekey=privatekeyfactory.generateprivate (PKCS8ENCODEDKEYSPEC); Cipher Privatecipher= Cipher.getinstance ("RSA");            Privatecipher.init (Cipher.encrypt_mode, Privatekey); byte[] result =privatecipher.dofinal (Src.getbytes ()); System.out.println ("Private key encryption, public key decryption--encryption:" +base64.encodebase64string (result)); //private key encryption, public key decryption--decryptionX509encodedkeyspec X509encodedkeyspec =NewX509encodedkeyspec (rsapublickey.getencoded ()); Keyfactory publickeyfactory= Keyfactory.getinstance ("RSA"); PublicKey PublicKey=publickeyfactory.generatepublic (X509ENCODEDKEYSPEC); Cipher Publiccipher= Cipher.getinstance ("RSA");            Publiccipher.init (Cipher.decrypt_mode, PublicKey); Result=publiccipher.dofinal (Result); System.out.println ("Private key encryption, public key decryption--decryption:" +NewString (result));

2, public key encryption, private key decryption:

//public key encryption, private key decryption---encryptionX509encodedkeyspec =NewX509encodedkeyspec (rsapublickey.getencoded ()); Publickeyfactory= Keyfactory.getinstance ("RSA"); PublicKey=publickeyfactory.generatepublic (X509ENCODEDKEYSPEC); Publiccipher= Cipher.getinstance ("RSA");            Publiccipher.init (Cipher.encrypt_mode, PublicKey); Result=publiccipher.dofinal (Src.getbytes ()); System.out.println ("Public key encryption, private key decryption---encryption:" +base64.encodebase64string (result)); //public key encryption, private key decryption---decryptionPkcs8encodedkeyspec =NewPkcs8encodedkeyspec (rsaprivatekey.getencoded ()); Privatekeyfactory= Keyfactory.getinstance ("RSA"); Privatekey=privatekeyfactory.generateprivate (PKCS8ENCODEDKEYSPEC); Privatecipher= Cipher.getinstance ("RSA");            Privatecipher.init (Cipher.decrypt_mode, Privatekey); Result=privatecipher.dofinal (Result); System.out.println ("Public key encryption, private key decryption---decryption:" +NewString (result));

Depending on the output, it is not difficult to find that the public key and private key are not consistent and the private key is longer than the public key.

To today's information encryption algorithm: BASE64, symmetric encryption algorithm, message digest encryption algorithm, asymmetric encryption algorithm is all summed up.

Asymmetric encryption algorithm of information encryption RSA

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.