利用RijndaelManaged加密解码。需要导入命名空间 using System.Security.Cryptography;
一个很简单的例子,最终的结果
解密和加密类
public class DESEncrypt { #region ========加密======== /// <summary> /// 加密数据 /// </summary> /// <param name="Text">要加密的内容</param> /// <param name="sKey">key,必须为32位</param> /// <returns></returns> public static string Encrypt(string Text, string sKey) { byte[] keyArray = UTF8Encoding.UTF8.GetBytes(sKey); RijndaelManaged encryption = new RijndaelManaged(); encryption.Key = keyArray; encryption.Mode = CipherMode.ECB; encryption.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = encryption.CreateEncryptor(); byte[] _EncryptArray = UTF8Encoding.UTF8.GetBytes(Text); byte[] resultArray = cTransform.TransformFinalBlock(_EncryptArray, 0, _EncryptArray.Length); return Convert.ToBase64String(resultArray, 0, resultArray.Length); } #endregion #region ========解密======== /// <summary> /// 解密数据 /// </summary> /// <param name="Text"></param> /// <param name="sKey"></param> /// <returns></returns> public static string Decrypt(string Text, string sKey) { byte[] keyArray = UTF8Encoding.UTF8.GetBytes(sKey); RijndaelManaged decipher = new RijndaelManaged(); decipher.Key = keyArray; decipher.Mode = CipherMode.ECB; decipher.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = decipher.CreateDecryptor(); byte[] _EncryptArray = Convert.FromBase64String(Text); byte[] resultArray = cTransform.TransformFinalBlock(_EncryptArray, 0, _EncryptArray.Length); return UTF8Encoding.UTF8.GetString(resultArray); } #endregion }
编写测试代码
1 using UnityEngine; 2 using System.Collections; 3 using System; 4 using System.Security.Cryptography; 5 using System.Text; 6 public class da : MonoBehaviour 7 { 8 9 //需要加密的字符串 10 string encrypt = string.Empty; 11 //加密后的字符串 12 string desEncrypt = string.Empty; 13 // Use this for initialization 14 void Start() 15 { 16 17 } 18 19 // Update is called once per frame 20 void Update() 21 { 22 23 } 24 25 26 void OnGUI() 27 { 28 string key = "12345678901234567890123456789000"; //必须为32位 29 encrypt = GUILayout.TextField(encrypt, GUILayout.MinWidth(150)); 30 GUILayout.Space(5); 31 desEncrypt = GUILayout.TextArea(desEncrypt, GUILayout.Height(200),GUILayout.MinWidth(150)); 32 33 if (GUILayout.Button("加密")) 34 { 35 desEncrypt = DESEncrypt.Encrypt(encrypt, key); 36 } 37 if (GUILayout.Button("解密")) 38 { 39 desEncrypt = DESEncrypt.Decrypt(desEncrypt, key); 40 } 41 } 42 }
或者可以用MD5
1 /// <summary> 2 /// MD5加密 3 /// </summary> 4 /// <param name="sDataIn"></param> 5 /// <returns></returns> 6 public static string GetMD5(string sDataIn) 7 { 8 MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); 9 byte[] bytValue, bytHash; 10 bytValue = System.Text.Encoding.UTF8.GetBytes(sDataIn + "md"); 11 bytHash = md5.ComputeHash(bytValue); 12 md5.Clear(); 13 string sTemp = ""; 14 for (int i = 0; i < bytHash.Length; i++) 15 { 16 sTemp += bytHash[i].ToString("X").PadLeft(2, '0'); 17 } 18 return sTemp.ToLower(); 19 }
收集的一个代码
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.Security.Cryptography; 5 using System.IO; 6 namespace GetMD5 7 { 8 class Program 9 { 10 public static string getFileHash(string filePath) 11 { 12 try 13 { 14 FileStream fs = new FileStream(filePath, FileMode.Open); 15 int len = (int)fs.Length; 16 byte[] data = new byte[len]; 17 fs.Read(data, 0, len); 18 fs.Close(); 19 MD5 md5 = new MD5CryptoServiceProvider(); 20 byte[] result = md5.ComputeHash(data); 21 string fileMD5 = ""; 22 foreach (byte b in result) 23 { 24 fileMD5 += Convert.ToString(b, 16); 25 } 26 return fileMD5; 27 } 28 catch (FileNotFoundException e) 29 { 30 Console.WriteLine(e.Message); 31 return ""; 32 } 33 } 34 static void Main(string[] args) 35 { 36 string md5 = getFileHash("E:\Myweb\cubetest.unity3d"); 37 string b = getFileHash("E:\Myweb\testscene.unity3d"); 38 Console.WriteLine(md5); 39 Console.WriteLine(b); 40 Console.ReadKey(); 41 42 } 43 } 44 }