12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- 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 < encryptData.Length; i++)
- {
- encryptData[i] = (byte)(encryptData[i] ^ key[i % key.Length]);
- }
- }
- return encryptData;
- }
- /// <summary>
- /// 获取解密的数据(无密钥的情况下直接获取数据)
- /// </summary>
- 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 < encryptData.Length; i++)
- {
- encryptData[i] = (byte)(encryptData[i] ^ secretKey[i % secretKey.Length]);
- }
- }
- }
- else
- {
- encryptData = null;
- }
- }
- return encryptData;
- }
- }
-
- }
|