Java RSA Plus decryption and purpose

Source: Internet
Author: User
Tags base64 decrypt

In the current version of the company's middleware communication framework, in order to prevent non-authorized third-party and expired client connection, we have AES and RSA two ways of encryption and decryption policy authentication. For asymmetric RSA encryption and decryption, because its performance is expensive, it is generally used only for authentication connection, not for each message itself encryption and decryption (this generally uses aes/des encryption), for the more secure payment channel, it is generally agreed to periodically exchange encryption key, The message of the exchange process itself is encrypted and decrypted by RSA. This provides a better guarantee on the basis of pure symmetric encryption, as long as the signatures are complex and regular updates are sufficient to make the cost of destruction high beyond the cost of cracking.

In general, the public key is published to the client, and the client courageously encrypts the message. The private key is used to decrypt the message. In the Java implementation of RSA, you can refer to the following:

ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;ImportJava.io.ObjectInputStream;ImportJava.io.ObjectOutputStream;ImportJava.math.BigInteger;ImportJava.security.KeyPair;ImportJava.security.KeyPairGenerator;ImportJava.security.PrivateKey;ImportJava.security.PublicKey;ImportJava.security.interfaces.RSAPrivateKey;ImportJava.security.interfaces.RSAPublicKey;Importorg.apache.commons.codec.binary.Base64; Public classRsautils {/*** Generate public and private keys, typically once generated, stored in files for distribution and use*/     Public Static voidGenerateKey () {Try{keypairgenerator KPG= Keypairgenerator.getinstance ("RSA"); Kpg.initialize (1024); KeyPair KP=Kpg.genkeypair (); PublicKey Pbkey=kp.getpublic (); Privatekey Prkey=kp.getprivate (); //Save Public KeyFileOutputStream F1 =NewFileOutputStream ("D:/pubkey.dat"); ObjectOutputStream B1=NewObjectOutputStream (F1);              B1.writeobject (Pbkey); //Save private keyFileOutputStream F2 =NewFileOutputStream ("D:/privatekey.dat"); ObjectOutputStream B2=NewObjectOutputStream (F2);          B2.writeobject (Prkey); } Catch(Exception e) {}}/*** Public key encryption, general caller pass plaintext, read public key from local store to encrypt *@paramPlaintxt *@return     * @throwsException*/     Public StaticString Pubencrypt (String plaintxt)throwsException {String s= Base64.encodebase64string (Plaintxt.getbytes ("UTF-8")); //get public key and parameter E,nFileInputStream f =NewFileInputStream ("D:/pubkey.dat"); ObjectInputStream b=NewObjectInputStream (f); Rsapublickey pbk=(Rsapublickey) b.readobject (); BigInteger e=pbk.getpublicexponent (); BigInteger N=Pbk.getmodulus (); //get clear Text m        bytePtext[] = s.getbytes ("UTF-8"); BigInteger m=NewBigInteger (Ptext); //Calculate ciphertext CBigInteger C =M.modpow (e, N); //Save RedactionString Cipertxt =c.tostring (); returnCipertxt; }        /*** Private key decryption, general caller pass ciphertext, read private key from local store to decrypt *@paramCipertxt *@return     * @throwsException*/     Public StaticString Privdecrypt (String cipertxt)throwsException {BigInteger C=NewBigInteger (cipertxt); //Read private keyFileInputStream f =NewFileInputStream ("D:/privatekey.dat"); ObjectInputStream b=NewObjectInputStream (f); Rsaprivatekey PRK=(Rsaprivatekey) b.readobject (); BigInteger D=prk.getprivateexponent (); //get private key parameters and decryptionBigInteger n =Prk.getmodulus (); BigInteger m=C.modpow (d, N); //Show decryption Results        byte[] Mt =M.tobytearray (); String Plaintxt=NewString (BASE64.DECODEBASE64 (MT), "UTF-8"); returnPlaintxt; }       Public Static voidMain (String args[]) {Try {              //GenerateKey (); String cipertxt = pubencrypt ("Test Greater China Area 123"); System.out.println ("Public Key cipher cipher:" +cipertxt); System.out.println ("Private key decryption:" +Privdecrypt (cipertxt)); } Catch(Exception e) {System.out.println (e.tostring ()); }      }}

PS: About which encryption, which decryption problem, remember the following principles (this is from a reasonable point of view, not purely technical):

Since it is encrypted, it must not want others to know my message, so only I can decrypt, so it can be concluded that the public key is responsible for encryption, the private key is responsible for decryption ; Similarly, since it is a signature, it must not want someone posing as I send the message, only I can publish this signature, so can draw The private key is responsible for signing and the public key is responsible for authentication .

Java RSA Plus decryption and purpose

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.