Java and C # RSA encryption and decryption communicate with each other and use public key encrypted transport

Source: Internet
Author: User
Tags modulus

For Java and C # encryption and decryption communication, you can use this Bouncycastle plugin, will help you solve a lot of problems

http://www.bouncycastle.org/

//C # uses Java-given public key for RSA encryption Public Static byte[] Rsaencrypt (stringPublicKey,stringcontent) {RSACryptoServiceProvider RSA=NewRSACryptoServiceProvider (); byte[] Publickeybytes =convert.frombase64string (PublicKey); Asymmetrickeyparameter Asymmetrickeyparameter=Publickeyfactory.createkey (publickeybytes); Rsakeyparameters rsakeyparameters=(rsakeyparameters) Asymmetrickeyparameter; RSAParameters RSAParameters=NewRSAParameters (); Rsaparameters.modulus=rsaKeyParameters.Modulus.ToByteArrayUnsigned (); Rsaparameters.exponent=rsaKeyParameters.Exponent.ToByteArrayUnsigned (); Rsa.        ImportParameters (RSAParameters); returnRsa. Encrypt (Encoding.GetEncoding ("GBK"). GetBytes (content),false); }

How to use:

string tempstr=convert.tobase64string (Rsacoder.rsaencrypt (" public key ""  characters that need to be added with a password "))

Ordinary C # itself calls and decrypts:

RSACryptoServiceProvider ORSA =NewRSACryptoServiceProvider (); stringPrivatekey = orsa.toxmlstring (true);//private Key            stringPublicKey = orsa.toxmlstring (false);//Public Key//these two keys need to be saved .            byte[] messagebytes = Encoding.UTF8.GetBytes ("Luo Luo");//data that needs to be encrypted//Public Key CryptographyRSACryptoServiceProvider oRSA1 =NewRSACryptoServiceProvider (); Orsa1.fromxmlstring (PublicKey); //encryption to use the public key so import the public key            byte[] Aoutput = Orsa1.encrypt (Messagebytes,false);//aoutput data after encryption//private Key DecryptionRSACryptoServiceProvider oRSA2 =NewRSACryptoServiceProvider ();            Orsa2.fromxmlstring (Privatekey); byte[] Ainput = Orsa2.decrypt (Aoutput,false); stringReslut = Encoding.UTF8.GetString (ainput);

Java and C # RSA encryption and decryption communicate with each other and use public key encrypted transport

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.