using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class RSAHelper : MonoBehaviour { private const string privateKey = "ANXINxf119!@"; //public string message = "xf119119!@我"; //private string msgAdd = ""; //private string msgDel = ""; void Start() { } // Update is called once per frame void Update() { //if (Input.GetKeyDown(KeyCode.T)) //{ // TipPanelManager.GetInstance.Show("ceshitongguo"); //} //if (Input.GetKeyDown(KeyCode.S)) //{ // WWW www = new WWW("https://www.baidu.com/"); // Application.OpenURL(www.url); //} //if (Input.GetKeyDown(KeyCode.Q)) //{ // msgAdd = RSAEncryption(message, privateKey); // Debug.Log("加密后信息:" + msgAdd); //} //if (Input.GetKeyDown(KeyCode.W)) //{ // msgDel = RSADecrypt(msgAdd, privateKey); // Debug.Log("解密后信息:" + msgDel); //} } /// /// RSA加密数据 /// /// 要加密数据 /// 密匙容器的名称 /// public static string RSAEncryption(string express, string KeyContainerName = null) { System.Security.Cryptography.CspParameters param = new System.Security.Cryptography.CspParameters(); param.KeyContainerName = KeyContainerName ?? privateKey; //密匙容器的名称,保持加密解密一致才能解密成功 using (System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider(param)) { byte[] plaindata = System.Text.Encoding.Default.GetBytes(express);//将要加密的字符串转换为字节数组 byte[] encryptdata = rsa.Encrypt(plaindata, false);//将加密后的字节数据转换为新的加密字节数组 return Convert.ToBase64String(encryptdata);//将加密后的字节数组转换为字符串 } } /// /// RSA解密数据 /// /// 要解密数据 /// 密匙容器的名称 /// public static string RSADecrypt(string ciphertext, string KeyContainerName = null) { System.Security.Cryptography.CspParameters param = new System.Security.Cryptography.CspParameters(); param.KeyContainerName = KeyContainerName ?? privateKey; //密匙容器的名称,保持加密解密一致才能解密成功 using (System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider(param)) { byte[] encryptdata = Convert.FromBase64String(ciphertext); byte[] decryptdata = rsa.Decrypt(encryptdata, false); return System.Text.Encoding.Default.GetString(decryptdata); } } }