作者:罗卡恩
链接:https://www.jianshu.com/p/114bfa976d00
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
MD5跟之前Rijndael算法差不多,不过在Web中传递数据应用很常见,安全性很高,它是一个不可逆的字符串变换算法,不过被山东大学的王小云教授破解了。然后再AB包里当做核对数据完整性。
看一看.Net官网怎么写的,官方是创建32个字节的十六进制格式的哈希字符串,然后我又稍加修改
using System;
using System.Text;
using System.Security.Cryptography;
/// <summary>
/// MD5加密类
/// </summary>
public class MD5
{
public static string GetMD5Hash(string str)
{
//就是比string往后一直加要好的优化容器
StringBuilder sb = new StringBuilder();
using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
{
//将输入字符串转换为字节数组并计算哈希。
byte[] data = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
//X为 十六进制 X都是大写 x都为小写
//2为 每次都是两位数
//假设有两个数10和26,正常情况十六进制显示0xA、0x1A,这样看起来不整齐,为了好看,可以指定"X2",这样显示出来就是:0x0A、0x1A。
//遍历哈希数据的每个字节
//并将每个字符串格式化为十六进制字符串。
int length = data.Length;
for (int i = 0; i < length; i++)
sb.Append(data[i].ToString("X2"));
}
return sb.ToString();
}
//验证
public static bool VerifyMD5Hash(string str,string hash)
{
string hashOfInput = GetMD5Hash(str);
if (hashOfInput.CompareTo(hash)==0)
return true;
else
return false;
}
}
测试
string str = "the best hop";
Debug.Log(str);
string newStr = MD5.GetMD5Hash(str);
Debug.Log(newStr);
Debug.Log(MD5.VerifyMD5Hash(str, newStr));
再加上一条,不同的字符串MD5值不同
string str = "the best hop";
Debug.Log(str);
string newStr = MD5.GetMD5Hash(str);
newStr = "aaaa";
Debug.Log(newStr);
Debug.Log(MD5.VerifyMD5Hash(str, newStr));
个人想法可能是这个套路使用MD5码,因为他是唯一的无法反转,然后注册时候服务器只存这个被MD5码处理后的密码,然后每次登录都用MD5码处理然后再服务器校对,这样可以保证的是公司不知道玩家的密码,但是可以去验证,保证了玩家的密码数据安全。