2025-04-23 10:01:05 +08:00
|
|
|
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);
|
2025-04-23 14:00:54 +08:00
|
|
|
if (File.Exists(filePath)) return "";
|
2025-04-23 10:01:05 +08:00
|
|
|
// 读取加密文件
|
|
|
|
|
byte[] encryptedData = File.ReadAllBytes(fullPath);
|
2025-04-24 14:54:32 +08:00
|
|
|
return ReadAndDecryptData(encryptedData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static string ReadAndDecryptData(byte[] encryptedData)
|
|
|
|
|
{
|
2025-04-23 10:01:05 +08:00
|
|
|
|
|
|
|
|
// 创建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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|