using System.IO; using System.Security.Cryptography; using System.Text; using UnityEngine; public class DecryptFileReader { private static byte[] key = Encoding.UTF8.GetBytes("Sixteen byte key"); // 加密密钥,需与加密时一致 private static byte[] iv = Encoding.UTF8.GetBytes("InitializationVe"); // 确保IV长度为16字节 public static string ReadAndDecryptData(string filePath) { string fullPath = Path.Combine(Application.streamingAssetsPath, filePath); if (File.Exists(filePath)) return ""; // 读取加密文件 byte[] encryptedData = File.ReadAllBytes(fullPath); return ReadAndDecryptData(encryptedData); } public static string ReadAndDecryptData(byte[] encryptedData) { // 创建AES解密器 using (Aes aesAlg = Aes.Create()) { aesAlg.Key = key; aesAlg.IV = iv; ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); // 创建内存流和加密流 using (MemoryStream msDecrypt = new MemoryStream(encryptedData)) { using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { byte[] decryptedBytes = new byte[encryptedData.Length]; int decryptedByteCount = csDecrypt.Read(decryptedBytes, 0, decryptedBytes.Length); // 将解密后的数据转换为字符串 string decryptedData = Encoding.UTF8.GetString(decryptedBytes, 0, decryptedByteCount); return decryptedData; } } } } }