· 6 years ago · Aug 17, 2019, 10:30 PM
1using UnityEngine;
2using System.Security.Cryptography;
3using System.Text;
4
5public class ProtectedPrefs {
6
7 public static void SetString(string key, string value) {
8 PlayerPrefs.SetString(md5(key), encrypt(value));
9 }
10
11 public static string GetString(string key, string defaultValue) {
12 if (!HasKey (key))
13 return defaultValue;
14 try {
15 string s = decrypt(PlayerPrefs.GetString(md5(key)));
16 return s;
17 }
18 catch {
19 return defaultValue;
20 }
21 }
22
23 public static string GetString(string key) {
24 return GetString(key, "");
25 }
26
27 public static void SetInt(string key, int value) {
28 PlayerPrefs.SetString(md5(key), encrypt(value.ToString()));
29 }
30
31 public static int GetInt(string key, int defaultValue) {
32 if (!HasKey (key))
33 return defaultValue;
34 try {
35 string s = decrypt(PlayerPrefs.GetString(md5(key)));
36 int i = int.Parse(s);
37 return i;
38 }
39 catch {
40 return defaultValue;
41 }
42 }
43
44 public static int GetInt(string key) {
45 return GetInt(key, 0);
46 }
47
48
49 public static void SetFloat(string key, float value) {
50 PlayerPrefs.SetString(md5(key), encrypt(value.ToString()));
51 }
52
53
54 public static float GetFloat(string key, float defaultValue) {
55 if (!HasKey (key))
56 return defaultValue;
57 try {
58 string s = decrypt(PlayerPrefs.GetString(md5(key)));
59 float f = float.Parse(s, System.Globalization.CultureInfo.InvariantCulture);
60 return f;
61 }
62 catch {
63 return defaultValue;
64 }
65 }
66
67 public static float GetFloat(string key) {
68 return GetFloat(key, 0);
69 }
70
71 public static bool HasKey(string key) {
72 return PlayerPrefs.HasKey(md5(key));
73 }
74
75 public static void DeleteAll() {
76 PlayerPrefs.DeleteAll();
77 }
78
79 public static void DeleteKey(string key) {
80 PlayerPrefs.DeleteKey(md5(key));
81 }
82
83 public static void Save() {
84 PlayerPrefs.Save ();
85 }
86
87
88 private static string secretKey = "1234567890";
89 private static byte[] key = new byte[8] {55, 13, 47, 24, 99, 111, 31, 67};
90 private static byte[] iv = new byte[8] {15, 28, 44, 30, 88, 12, 8, 217};
91
92 private static string encrypt(string s)
93 {
94 byte[] inputbuffer = Encoding.Unicode.GetBytes(s);
95 byte[] outputBuffer = DES.Create().CreateEncryptor(key, iv).TransformFinalBlock(inputbuffer, 0, inputbuffer.Length);
96 return System.Convert.ToBase64String(outputBuffer);
97 }
98
99 private static string decrypt(string s) {
100 byte[] inputbuffer = System.Convert.FromBase64String(s);
101 byte[] outputBuffer = DES.Create().CreateDecryptor(key, iv).TransformFinalBlock(inputbuffer, 0, inputbuffer.Length);
102 return Encoding.Unicode.GetString(outputBuffer);
103 }
104
105 private static string md5(string s) {
106 byte[] hashBytes = new MD5CryptoServiceProvider().ComputeHash(new UTF8Encoding().GetBytes(s + secretKey + SystemInfo.deviceUniqueIdentifier));
107 string hashString = "";
108 for (int i = 0; i < hashBytes.Length; i++) {
109 hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
110 }
111 return hashString.PadLeft(32, '0');
112 }
113}