using UnityEngine; using System.Collections; using System.Security.Cryptography; using System.Text; using System; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Security; public class RSAPro { string publickey; string privatekey; string RSAed; public string mingwen; RSACryptoServiceProvider rsa; /// /// 加密 /// /// 公钥 /// 所加密的内容 /// 加密后的内容 public static string RSAEncrypt(string publickey, string content) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); byte[] cipherbytes; rsa.FromXmlString(publickey); //rsa.FromXmlString(""+publickey+""); cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false); return Convert.ToBase64String(cipherbytes); ; } /// /// 解密 /// /// 私钥 /// 加密后的内容 /// 解密后的内容 public static string RSADecrypt(string privatekey, string content) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); byte[] cipherbytes; rsa.FromXmlString(privatekey); cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false); return Encoding.UTF8.GetString(cipherbytes); } /// /// base64 public key string -> xml public key /// /// /// public static string ToXmlPublicKey(string pubilcKey) { RsaKeyParameters p =PublicKeyFactory.CreateKey(Convert.FromBase64String(pubilcKey)) as RsaKeyParameters; using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { RSAParameters rsaParams = new RSAParameters { Modulus = p.Modulus.ToByteArrayUnsigned(), Exponent = p.Exponent.ToByteArrayUnsigned() }; rsa.ImportParameters(rsaParams); return rsa.ToXmlString(false); } } }