In unity, you can use the c # built-in symmetric algorithm to encrypt data. The following two encryption algorithms are available:
Using System; using System. text; using System. security. cryptography; using System. IO; using UnityEngine; public class StringEncryption {# region method C # For string encryption and decryption (symmetric algorithm) private static byte [] Keys = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; /// <summary> // DES encrypted string /// </summary> /// <param name = "encryptString"> string to be encrypted </param> // /<param name = "encryptKey"> encryption key, the value must be 8-bit </param> /// <returns>. The encrypted string is returned successfully, and the source string </returns> public static string EncryptDES (string encryptString, string encryptKey) {try {byte [] rgbKey = Encoding. UTF8.GetBytes (encryptKey. substring (0, 8); byte [] rgbIV = Keys; byte [] inputByteArray = Encoding. UTF8.GetBytes (encryptString); DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider (); MemoryStream mStream = new MemoryStream (); CryptoStream cStream = new CryptoStream (mStream, dCSP. createEncryptor (rgbKey, rgbIV), CryptoStreamMode. write); cStream. write (inputByteArray, 0, inputByteArray. length); cStream. flushFinalBlock (); cStream. close (); return Convert. toBase64String (mStream. toArray ();} catch {return encryptString ;}} /// <summary> // DES decryption string /// </summary> /// <param name = "decryptString"> string to be decrypted </param> // /<param name = "decryptKey"> decrypt the key, the value must be 8 bits. It must be the same as the encryption key. </param> /// <returns> the decrypted string is returned, returned source string failed </returns> public static string DecryptDES (string decryptString, string decryptKey) {try {byte [] rgbKey = Encoding. UTF8.GetBytes (decryptKey); byte [] rgbIV = Keys; byte [] inputByteArray = Convert. fromBase64String (decryptString); DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider (); MemoryStream mStream = new MemoryStream (); CryptoStream cStream = new CryptoStream (mStream, DCSP. createDecryptor (rgbKey, rgbIV), CryptoStreamMode. write); cStream. write (inputByteArray, 0, inputByteArray. length); cStream. flushFinalBlock (); cStream. close (); return Encoding. UTF8.GetString (mStream. toArray ();} catch {Debug. log ("catch"); return decryptString ;}# endregion # region MD5 irreversible encryption // 32-bit encryption public string GetMD5_32 (string s, string _ input_charset) {MD5 md5 = new MD5CryptoServiceProvider (); byte [] t = md5.ComputeHash (Encoding. getEncoding (_ input_charset ). getBytes (s); StringBuilder sb = new StringBuilder (32); for (int I = 0; I <t. length; I ++) {sb. append (t [I]. toString ("x "). padLeft (2, '0');} return sb. toString () ;}// 16-bit encryption public static string GetMd5_16 (string ConvertString) {MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider (); string t2 = BitConverter. toString (md5.ComputeHash (UTF8Encoding. default. getBytes (ConvertString), 4, 8); t2 = t2.Replace ("-", ""); return t2 ;}# endregion}