Skip to content

Instantly share code, notes, and snippets.

@ftvs
Last active March 21, 2024 08:38
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save ftvs/5299600 to your computer and use it in GitHub Desktop.
Save ftvs/5299600 to your computer and use it in GitHub Desktop.
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");
}
}
@chiuan
Copy link

chiuan commented Mar 29, 2019

nice support! good job

@hakkikonu
Copy link

can you show function call examples on a real case

@mquaker
Copy link

mquaker commented Sep 26, 2019

can you show function call examples on a real case

You can use it in the same way as playerprefs method.

EncryptedPlayerPrefs. SetInt( "High_Score", 10 );
int _highscore = EncryptedPlayerPrefs. GetInt( "High_Score", 0 );

@GMyjak
Copy link

GMyjak commented Jul 6, 2021

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.

@eavme
Copy link

eavme commented Feb 16, 2022

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