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