| using System; |
| using System.IO; |
| using System.Security.Cryptography; |
| using System.Text; |
| using UnityEngine; |
| |
| public static class EncryptionUtils |
| { |
| private static readonly string encryptionKey = "YourEncryptionKey"; // 更换为你的密钥 |
| |
| public static byte[] Encrypt(byte[] data) |
| { |
| using (Aes aes = Aes.Create()) |
| { |
| aes.Key = Encoding.UTF8.GetBytes(encryptionKey); |
| aes.GenerateIV(); |
| using (MemoryStream ms = new MemoryStream()) |
| { |
| ms.Write(aes.IV, 0, aes.IV.Length); |
| using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write)) |
| { |
| cs.Write(data, 0, data.Length); |
| cs.FlushFinalBlock(); |
| } |
| return ms.ToArray(); |
| } |
| } |
| } |
| |
| public static byte[] Decrypt(byte[] data) |
| { |
| using (Aes aes = Aes.Create()) |
| { |
| aes.Key = Encoding.UTF8.GetBytes(encryptionKey); |
| using (MemoryStream ms = new MemoryStream(data)) |
| { |
| byte[] iv = new byte[aes.IV.Length]; |
| ms.Read(iv, 0, iv.Length); |
| aes.IV = iv; |
| using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Read)) |
| { |
| using (MemoryStream output = new MemoryStream()) |
| { |
| cs.CopyTo(output); |
| return output.ToArray(); |
| } |
| } |
| } |
| } |
| } |
| } |
| |
| public class AssetBundleManager : MonoBehaviour |
| { |
| public string bundleName; |
| private string bundlePath; |
| private string decryptedBundlePath; |
| |
| void Start() |
| { |
| bundlePath = Application.streamingAssetsPath + "/" + bundleName + ".assetbundle"; |
| decryptedBundlePath = Application.persistentDataPath + "/" + bundleName + ".decrypted.assetbundle"; |
| // 加载并解密AssetBundle |
| LoadAndDecryptAssetBundle(); |
| // 加载解密后的资源(示例) |
| // AssetBundle bundle = AssetBundle.LoadFromFile(decryptedBundlePath); |
| // if (bundle != null) |
| // { |
| // GameObject prefab = bundle.LoadAsset<GameObject>("YourPrefabName"); |
| // Instantiate(prefab); |
| // bundle.Unload(false); |
| // } |
| } |
| |
| private void LoadAndDecryptAssetBundle() |
| { |
| if (!File.Exists(decryptedBundlePath)) |
| { |
| byte[] encryptedData = File.ReadAllBytes(bundlePath); |
| byte[] decryptedData = EncryptionUtils.Decrypt(encryptedData); |
| File.WriteAllBytes(decryptedBundlePath, decryptedData); |
| } |
| } |
| } |