-
-
Save mquaker/bd9c12d2e02b0bd28654bc70d844a4a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Security.Cryptography; | |
using System.Text; | |
public class EncryptedPlayerPrefs { | |
// Encrypted PlayerPrefs | |
// Written by Sven Magnus | |
// MD5 code by Matthew Wegner (from [url]http://www.unifycommunity.com/wiki/index.php?title=MD5[/url]) | |
// Modify this key in this file : | |
private static string privateKey="9ETrEsWaFRach3gexaDr"; | |
// Add some values to this array before using EncryptedPlayerPrefs | |
public static string[] keys; | |
public static string Md5(string strToEncrypt) { | |
UTF8Encoding ue = new UTF8Encoding(); | |
byte[] bytes = ue.GetBytes(strToEncrypt); | |
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); | |
byte[] hashBytes = md5.ComputeHash(bytes); | |
string hashString = ""; | |
for (int i = 0; i < hashBytes.Length; i++) { | |
hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0'); | |
} | |
return hashString.PadLeft(32, '0'); | |
} | |
public static void SaveEncryption(string key, string type, string value) { | |
int keyIndex = (int)Mathf.Floor(Random.value * keys.Length); | |
string secretKey = keys[keyIndex]; | |
string check = Md5(type + "_" + privateKey + "_" + secretKey + "_" + value); | |
PlayerPrefs.SetString(key + "_encryption_check", check); | |
PlayerPrefs.SetInt(key + "_used_key", keyIndex); | |
} | |
public static bool CheckEncryption(string key, string type, string value) { | |
int keyIndex = PlayerPrefs.GetInt(key + "_used_key"); | |
string secretKey = keys[keyIndex]; | |
string check = Md5(type + "_" + privateKey + "_" + secretKey + "_" + value); | |
if(!PlayerPrefs.HasKey(key + "_encryption_check")) return false; | |
string storedCheck = PlayerPrefs.GetString(key + "_encryption_check"); | |
return storedCheck == check; | |
} | |
public static void SetInt(string key, int value) { | |
PlayerPrefs.SetInt(key, value); | |
SaveEncryption(key, "int", value.ToString()); | |
} | |
public static void SetFloat(string key, float value) { | |
PlayerPrefs.SetFloat(key, value); | |
SaveEncryption(key, "float", Mathf.Floor(value*1000).ToString()); | |
} | |
public static void SetString(string key, string value) { | |
PlayerPrefs.SetString(key, value); | |
SaveEncryption(key, "string", value); | |
} | |
public static int GetInt(string key) { | |
return GetInt(key, 0); | |
} | |
public static float GetFloat(string key) { | |
return GetFloat(key, 0f); | |
} | |
public static string GetString(string key) { | |
return GetString(key, ""); | |
} | |
public static int GetInt(string key,int defaultValue) { | |
int value = PlayerPrefs.GetInt(key); | |
if(!CheckEncryption(key, "int", value.ToString())) return defaultValue; | |
return value; | |
} | |
public static float GetFloat(string key, float defaultValue) { | |
float value = PlayerPrefs.GetFloat(key); | |
if(!CheckEncryption(key, "float", Mathf.Floor(value*1000).ToString())) return defaultValue; | |
return value; | |
} | |
public static string GetString(string key, string defaultValue) { | |
string value = PlayerPrefs.GetString(key); | |
if(!CheckEncryption(key, "string", value)) return defaultValue; | |
return value; | |
} | |
public static bool HasKey(string key) { | |
return PlayerPrefs.HasKey(key); | |
} | |
public static void DeleteKey(string key) { | |
PlayerPrefs.DeleteKey(key); | |
PlayerPrefs.DeleteKey(key + "_encryption_check"); | |
PlayerPrefs.DeleteKey(key + "_used_key"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment