Encrypted Player Prefs System UNITY

👤 Sharing: AI
using System;
using System.IO;
using System.Security.Cryptography;
using System.Threading.Tasks;
using System.Text;
using UnityEngine;

public class EncryptedPlayerPrefs : MonoBehaviour
{
    static string encryptionKey = "00000000000000000000000000000000"; // 32 byte (256 bit) anahtar
    static string initializationVector = "0000000000000000"; // 16 byte (128 bit) IV

    static async Task SavePlayerPrefsAsync() 
    { 
        await Task.Run(() => 
        { // PlayerPrefs'i kaydetme işlemi 
       // PlayerPrefs.Save(); 
        });
        
    }
    // Veri saklama işlemleri
    public static void SetInt(string key, int value)
    {
        string encryptedKey = Encrypt(key);
        string encryptedValue = Encrypt(value.ToString());
        PlayerPrefs.SetString(encryptedKey, encryptedValue);

        _ = SavePlayerPrefsAsync();
       
    }

    public static int GetInt(string key, int defaultValue = 0)
    {
        string encryptedKey = Encrypt(key);
        if (PlayerPrefs.HasKey(encryptedKey))
        {
            string encryptedValue = PlayerPrefs.GetString(encryptedKey);
            string decryptedValue = Decrypt(encryptedValue);
            return int.Parse(decryptedValue);
        }
        return defaultValue;
    }

    public static void SetFloat(string key, float value)
    {
        string encryptedKey = Encrypt(key);
        string encryptedValue = Encrypt(value.ToString());
        PlayerPrefs.SetString(encryptedKey, encryptedValue);
        #pragma warning disable CS4014 
            SavePlayerPrefsAsync();
        #pragma warning restore CS4014 
    }

    public static float GetFloat(string key, float defaultValue = 0f)
    {
        string encryptedKey = Encrypt(key);
        if (PlayerPrefs.HasKey(encryptedKey))
        {
            string encryptedValue = PlayerPrefs.GetString(encryptedKey);
            string decryptedValue = Decrypt(encryptedValue);
            return float.Parse(decryptedValue);
        }
        return defaultValue;
    }

    public static void SetString(string key, string value)
    {
        string encryptedKey = Encrypt(key);
        string encryptedValue = Encrypt(value);
        PlayerPrefs.SetString(encryptedKey, encryptedValue);
        #pragma warning disable CS4014 
            SavePlayerPrefsAsync();
        #pragma warning restore CS4014 
    }

    public static string GetString(string key, string defaultValue = "")
    {
        string encryptedKey = Encrypt(key);
        if (PlayerPrefs.HasKey(encryptedKey))
        {
            string encryptedValue = PlayerPrefs.GetString(encryptedKey);
            return Decrypt(encryptedValue);
        }
        return defaultValue;
    }

    public static void SetBool(string key, bool value)
    {
        string encryptedKey = Encrypt(key);
        string encryptedValue = Encrypt(value ? "1" : "0");
        PlayerPrefs.SetString(encryptedKey, encryptedValue);
        #pragma warning disable CS4014 
            SavePlayerPrefsAsync();
        #pragma warning restore CS4014 
    }

    public static bool GetBool(string key, bool defaultValue = false)
    {
        string encryptedKey = Encrypt(key);
        if (PlayerPrefs.HasKey(encryptedKey))
        {
            string encryptedValue = PlayerPrefs.GetString(encryptedKey);
            return Decrypt(encryptedValue) == "1";
        }
        return defaultValue;
    }

    public static void DeleteKey(string key)
    {
        string encryptedKey = Encrypt(key);
        PlayerPrefs.DeleteKey(encryptedKey);
        #pragma warning disable CS4014 
            SavePlayerPrefsAsync();
        #pragma warning restore CS4014 
    }

    public static bool HasKey(string key)
    {
        string encryptedKey = Encrypt(key);
        return PlayerPrefs.HasKey(encryptedKey);
    }

    // Şifreleme ve çözme işlemleri
    private static string Encrypt(string data)
    {
        try
        {
            byte[] dataBytes = Encoding.UTF8.GetBytes(data);
            using (Aes aes = Aes.Create())
            {
                aes.Key = Encoding.UTF8.GetBytes(encryptionKey);
                aes.IV = Encoding.UTF8.GetBytes(initializationVector);

                using (MemoryStream memoryStream = new MemoryStream())
                {
                    using (CryptoStream cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cryptoStream.Write(dataBytes, 0, dataBytes.Length);
                    }
                    return Convert.ToBase64String(memoryStream.ToArray());
                }
            }
        }
        catch (Exception e)
        {
            Debug.LogError("Error in Encrypt: " + e.Message);
            return string.Empty;
        }
    }

    private static string Decrypt(string data)
    {
        try
        {
            byte[] dataBytes = Convert.FromBase64String(data);
            using (Aes aes = Aes.Create())
            {
                aes.Key = Encoding.UTF8.GetBytes(encryptionKey);
                aes.IV = Encoding.UTF8.GetBytes(initializationVector);

                using (MemoryStream memoryStream = new MemoryStream())
                {
                    using (CryptoStream cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cryptoStream.Write(dataBytes, 0, dataBytes.Length);
                    }
                    return Encoding.UTF8.GetString(memoryStream.ToArray());
                }
            }
        }
        catch (Exception e)
        {
            Debug.LogError("Error in Decrypt: " + e.Message);
            return string.Empty;
        }
    }
}
👁️ Viewed: 83

Comments