using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using UnityEngine;
public class CryptoPrefs
{
private static string sKEY = "ZTdkNTNmNDE2NTM3MWM0NDFhNTEzNzU1";
private static string sIV = "4rZymEMfa/PpeJ89qY4gyA==";
public static void SetInt(string key, int val)
{
PlayerPrefs.SetString(CryptoPrefs.GetHash(key), CryptoPrefs.Encrypt(val.ToString()));
}
public static int GetInt(string key, int defaultValue = 0)
{
string @string = CryptoPrefs.GetString(key, defaultValue.ToString());
int result = defaultValue;
int.TryParse(@string, out result);
return result;
}
public static void SetFloat(string key, float val)
{
PlayerPrefs.SetString(CryptoPrefs.GetHash(key), CryptoPrefs.Encrypt(val.ToString()));
}
public static float GetFloat(string key, float defaultValue = 0f)
{
string @string = CryptoPrefs.GetString(key, defaultValue.ToString());
float result = defaultValue;
float.TryParse(@string, out result);
return result;
}
public static void SetString(string key, string val)
{
PlayerPrefs.SetString(CryptoPrefs.GetHash(key), CryptoPrefs.Encrypt(val));
}
public static string GetString(string key, string defaultValue = "")
{
string text = defaultValue;
string @string = PlayerPrefs.GetString(CryptoPrefs.GetHash(key), defaultValue.ToString());
if (!text.Equals(@string))
{
text = CryptoPrefs.Decrypt(@string);
}
return text;
}
public static bool HasKey(string key)
{
string hash = CryptoPrefs.GetHash(key);
return PlayerPrefs.HasKey(hash);
}
public static void DeleteKey(string key)
{
string hash = CryptoPrefs.GetHash(key);
PlayerPrefs.DeleteKey(hash);
}
public static void DeleteAll()
{
PlayerPrefs.DeleteAll();
}
public static void Save()
{
PlayerPrefs.Save();
}
private static string Decrypt(string encString)
{
RijndaelManaged rijndaelManaged = new RijndaelManaged
{
Padding = PaddingMode.Zeros,
Mode = CipherMode.CBC,
KeySize = 128,
BlockSize = 128
};
byte[] bytes = Encoding.UTF8.GetBytes(CryptoPrefs.sKEY);
byte[] rgbIV = Convert.FromBase64String(CryptoPrefs.sIV);
ICryptoTransform transform = rijndaelManaged.CreateDecryptor(bytes, rgbIV);
byte[] array = Convert.FromBase64String(encString);
byte[] array2 = new byte[array.Length];
MemoryStream stream = new MemoryStream(array);
CryptoStream cryptoStream = new CryptoStream(stream, transform, CryptoStreamMode.Read);
cryptoStream.Read(array2, 0, array2.Length);
return Encoding.UTF8.GetString(array2).TrimEnd(new char[1]);
}
private static string Encrypt(string rawString)
{
RijndaelManaged rijndaelManaged = new RijndaelManaged
{
Padding = PaddingMode.Zeros,
Mode = CipherMode.CBC,
KeySize = 128,
BlockSize = 128
};
byte[] bytes = Encoding.UTF8.GetBytes(CryptoPrefs.sKEY);
byte[] rgbIV = Convert.FromBase64String(CryptoPrefs.sIV);
ICryptoTransform transform = rijndaelManaged.CreateEncryptor(bytes, rgbIV);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write);
byte[] bytes2 = Encoding.UTF8.GetBytes(rawString);
cryptoStream.Write(bytes2, 0, bytes2.Length);
cryptoStream.FlushFinalBlock();
byte[] inArray = memoryStream.ToArray();
return Convert.ToBase64String(inArray);
}
private static string GetHash(string key)
{
MD5 mD = new MD5CryptoServiceProvider();
byte[] array = mD.ComputeHash(Encoding.UTF8.GetBytes(key));
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < array.Length; i++)
{
stringBuilder.Append(array[i].ToString("x2"));
}
return stringBuilder.ToString();
}
}
Unity3d加密PlayerPrefs
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Unity3D 项目加密防止反编译可使用Virbox LM加密方案。对Unity3D游戏项目中的代码及资源保护,V...
- Unity3D项目加密 Unity3D项目研发完成,发布前开发者需要对代码及资源进行加密,防止代码被反编译,防止资...
- 欢迎各位大神加入QQ群技术交流:730803578 Unity3D加密分析 Unity3D是一个基于Mono框架的...