123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- 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;
- }
- }
- /// <summary>
- /// 创建加密过的数据
- /// </summary>
- 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;
- }
- /// <summary>
- /// 获取解密的数据(无密钥的情况下直接获取数据)
- /// </summary>
- internal static byte[] GetDecryptData(string filePath, char[] secretKey = null)
- {
- //Logger.I($"GetDecryptData filePath : {filePath} secretKey : {secretKey}");
- byte[] encryptData;
- if (filePath.IndexOf("storage/emulated") > 0)
- {
- encryptData = File.ReadAllBytes(filePath);
- if (secretKey != null)
- {
- for (int i = 0; i < secretKey.Length; i++)
- {
- encryptData[i] = (byte)(encryptData[i] ^ secretKey[i % secretKey.Length]);
- }
- }
- }
- else
- {
- using (UnityWebRequest webRequest = UnityWebRequest.Get(filePath))
- {
- webRequest.SendWebRequest();
- while (!webRequest.isDone) { }
- #if UNITY_2020_1_OR_NEWER
- //Logger.I($"GetDecryptData filePath : 1 webRequest.isDone : {webRequest.isDone} webRequest.result : {webRequest.result}");
- if (webRequest.result == UnityWebRequest.Result.Success)
- #else
- Logger.I($"GetDecryptData filePath : 2");
- if (string.IsNullOrEmpty(webRequest.error))
- #endif
- {
- //Logger.I($"GetDecryptData filePath : 3");
- 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
- {
- //Logger.I($"GetDecryptData filePath : 4");
- encryptData = null;
- }
- }
- }
- return encryptData;
- }
- }
- }
|