Quantcast
Channel: CodeSection,代码区,网络安全 - CodeSec
Viewing all articles
Browse latest Browse all 12749

Unity3D实战之文件加密

$
0
0

移动端游戏经常被一些玩家破解成白包,但是为了安全性,开发者还是需要使用算法对文本文件加密,加密的算法非常多,比如通常使用的是MD5算法,OBFS算法,SHA512算法等。由于MD5算法经常使用,网上也有现成的代码本节就直接掠过,直接讲OBFS,SHA512加密算法,为了便于大家理解,先把加密算法代码奉献上,加密函数代码如下所示:

//OBFS加密算法 private static string OBFS(string str) { int length = str.Length; var array = new char[length]; for (int i = 0; i < array.Length; i++) { char c = str[i]; var b = (byte)(c ^ length - i); var b2 = (byte)((c >> 8) ^ i); array[i] = (char)(b2 << 8 | b); } return new string(array); } //SHA512加密算法 public static string GetSHA512Password(string password) { byte[] bytes = Encoding.UTF7.GetBytes(password); byte[] result; SHA512 shaM = new SHA512Managed(); result = shaM.ComputeHash(bytes); StringBuilder sb = new StringBuilder(); foreach (byte num in result) { sb.AppendFormat("{0:x2}", num); } return sb.ToString(); }

以上两个算法实现了文本文件的密码,函数的参数是开发者自己定义的字符串,然后在该字符串的基础上通过算法加密生成新的字符串用于压缩文件加密。下面列出配置文件,并且调用OBFS函数或者是SHA512函数对文件进行加密,返回的是经过加密字符串,同时调用函数SaveConfigXMLTOZip对文件进行压缩加密。对应的函数语句如下所示:

private const string configurationFile = "config.txt"; private const string localizationFile = "translations.txt"; private /*const*/ string configurationZipPwd = OBFS("~;о__"); private /*const*/ string configurationZipPwd = GetSHA512Password("※%

Viewing all articles
Browse latest Browse all 12749

Latest Images

Trending Articles





Latest Images