76 lines
2.1 KiB
C#
76 lines
2.1 KiB
C#
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<T>(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<T>(json);
|
|
}
|
|
} |