using System; using System.IO; using System.Security.Cryptography; using System.Text; using Newtonsoft.Json; using UnityEngine; public static class CryptoUtil { private static readonly string Key = "7gT9KfL2xQ1bN4pV6sH8jD3zW0cR5mYq"; // 32바이트 private static readonly string IV = "aB3dE6gH9jK2mP5Q"; // 16바이트 public static string Encrypt(string plainText) { using (Aes aes = Aes.Create()) { aes.Key = Encoding.UTF8.GetBytes(Key); aes.IV = Encoding.UTF8.GetBytes(IV); using (var ms = new MemoryStream()) using (var cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write)) using (var sw = new StreamWriter(cs)) { sw.Write(plainText); sw.Close(); return Convert.ToBase64String(ms.ToArray()); } } } public static string Decrypt(string cipherText) { byte[] buffer = Convert.FromBase64String(cipherText); using (Aes aes = Aes.Create()) { aes.Key = Encoding.UTF8.GetBytes(Key); aes.IV = Encoding.UTF8.GetBytes(IV); using (var ms = new MemoryStream(buffer)) using (var cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Read)) using (var sr = new StreamReader(cs)) { return sr.ReadToEnd(); } } } public static void Save(object data, string filename) { string json = JsonConvert.SerializeObject(data); // 암호화 string encrypted = Encrypt(json); string path = Path.Combine(Application.persistentDataPath, filename); File.WriteAllText(path, encrypted); //Debug.Log("Saved to: " + path); } public static T Load(string filename) { string path = Path.Combine(Application.persistentDataPath, filename); if (File.Exists(path) == false) return default; string encrypted = File.ReadAllText(path); string json = Decrypt(encrypted); return JsonConvert.DeserializeObject(json, new JsonSerializerSettings { ObjectCreationHandling = ObjectCreationHandling.Replace }); } public static void Del(string filename) { string path = Path.Combine(Application.persistentDataPath, filename); File.Delete(path); } }