Last active
May 2, 2024 04:42
-
-
Save ftvs/5299600 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"); | |
} | |
} |
Have you even checked the script? He doesn't work either. More precisely, encryption works crookedly. I don't recommend using it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When I look at the code, it doesn't seem like these PlayerPrefs are encrypted. In Set methods you save data in plain text, in Get methods you read them as plain text and check md5. The only thing you can accomplish is make you data tamper proof. So I think script name is misleading.
By the way, if you try to prevent users from modyfing PlayerPrefs manually, Get methods should probably throw exception instead of returning default value.