using NUnit.Framework; using System; using System.Collections.Generic; using UnityEngine; using System.IO; using System.Security.Cryptography; using System.Text; public class DataStorageManager : MonoBehaviour { public static DataStorageManager Instance; public LeaderboardData leaderboardData; //yes this is hardcoded but u should use a secure key management system //though I wont cover here that because if u decompile the software, search in the code and create a //script to decrypt the json just to modify the scores to cheat in this stupid game, my man, u have problems bigger than that ngl private byte[] key = Encoding.UTF8.GetBytes("1234567890123456"); private byte[] iv = Encoding.UTF8.GetBytes("6543210987654321"); void Awake() { if (Instance == null) Instance = this; else Destroy(gameObject); } public void SaveScores(List scores) { leaderboardData = new LeaderboardData(scores); string json = JsonUtility.ToJson(leaderboardData); string hash = ComputeHash(json); SaveWrapper wrapper = new SaveWrapper { json = json, hash = hash }; string wrapperJson = JsonUtility.ToJson(wrapper); string encrypted = Encrypt(wrapperJson); File.WriteAllText(Path.Combine(Application.persistentDataPath, "save.dat"), encrypted); Debug.Log("Secure save written"); } public List Load() { if (!File.Exists(Path.Combine(Application.persistentDataPath, "save.dat"))) return new List(); string encrypted = File.ReadAllText(Path.Combine(Application.persistentDataPath, "save.dat")); string decrypted = Decrypt(encrypted); SaveWrapper wrapper = JsonUtility.FromJson(decrypted); string expectedHash = ComputeHash(wrapper.json); if (expectedHash != wrapper.hash) { Debug.LogWarning("Save file tampered!"); return new List(); } LeaderboardData data = JsonUtility.FromJson(wrapper.json); Debug.Log("Secure save loaded successfully"); return data.topScores; } private string Encrypt(string plainText) { using (Aes aes = Aes.Create()) { aes.Key = key; aes.IV = iv; ICryptoTransform encryptor = aes.CreateEncryptor(); byte[] inputBytes = Encoding.UTF8.GetBytes(plainText); byte[] encrypted = encryptor.TransformFinalBlock(inputBytes, 0, inputBytes.Length); return System.Convert.ToBase64String(encrypted); } } private string Decrypt(string cipherText) { using (Aes aes = Aes.Create()) { aes.Key = key; aes.IV = iv; ICryptoTransform decryptor = aes.CreateDecryptor(); byte[] inputBytes = Convert.FromBase64String(cipherText); byte[] decrypted = decryptor.TransformFinalBlock( inputBytes, 0, inputBytes.Length ); return Encoding.UTF8.GetString(decrypted); } } private string ComputeHash(string input) { using (SHA256 sha = SHA256.Create()) { byte[] bytes = sha.ComputeHash(Encoding.UTF8.GetBytes(input)); return System.Convert.ToBase64String(bytes); } } } public class SaveWrapper { public string json; public string hash; } [Serializable] public class LeaderboardData { public List topScores = new List(); public LeaderboardData(List newScores) { topScores = newScores; } }