using System.Collections; using System.IO; using ET; using UnityEngine; using UnityEngine.Networking; namespace VEngine { public class EncryptHelper { public static string resKey; private static char[] _resKeyChars; public static char[] resKeyChars { get { if(_resKeyChars == null && !string.IsNullOrEmpty(resKey)) { _resKeyChars = resKey.ToCharArray(); } return _resKeyChars; } } /// /// 创建加密过的数据 /// public static byte[] CreateEncryptData(string filePath, string secretKey) { byte[] encryptData; char[] key = secretKey.ToCharArray(); using (FileStream fs = new FileStream(filePath, FileMode.Open)) { encryptData = new byte[fs.Length]; fs.Read(encryptData, 0, encryptData.Length); for (int i = 0; i < key.Length; i++) { encryptData[i] = (byte)(encryptData[i] ^ key[i % key.Length]); } } return encryptData; } /// /// 获取解密的数据(无密钥的情况下直接获取数据) /// internal static byte[] GetDecryptData(string filePath, char[] secretKey = null) { byte[] encryptData; using (UnityWebRequest webRequest = UnityWebRequest.Get(filePath)) { webRequest.SendWebRequest(); while (!webRequest.isDone) { } #if UNITY_2020_1_OR_NEWER if (webRequest.result == UnityWebRequest.Result.Success) #else if (string.IsNullOrEmpty(webRequest.error)) #endif { encryptData = webRequest.downloadHandler.data; if (secretKey != null) { for (int i = 0; i < secretKey.Length; i++) { encryptData[i] = (byte)(encryptData[i] ^ secretKey[i % secretKey.Length]); } } } else { encryptData = null; } } return encryptData; } } }