public static string getMD5(string str)//该方法获取字符串的md5加密 通经常使用来验证数据
{
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] pwd = md5.ComputeHash(Encoding.ASCII.GetBytes(str));
string ret = "";
for (int i = 0; i < pwd.Length; i++)
{
ret += pwd[i].ToString("X");
}
return ret;
}
private static string GetMD5HashFromFile(string fileName)//该方法用来进行获取文件的md5加密 用来比对 client文件是否被篡改
{
try
{
FileStream file = new FileStream(fileName, FileMode.Open);
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] retVal = md5.ComputeHash(file);
file.Close();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < retVal.Length; i++)
{
// sb.Append(retVal[i].ToString("x2"));//每两位转换为16进制、
sb.Append(retVal[i].ToString("X2"));//这儿必须是x2 才行 不然转换的时候 0Xeasy转为X 0 会被去掉、
}
return sb.ToString();
}
catch (Exception ex)
{
throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message);
}
}
{
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] pwd = md5.ComputeHash(Encoding.ASCII.GetBytes(str));
string ret = "";
for (int i = 0; i < pwd.Length; i++)
{
ret += pwd[i].ToString("X");
}
return ret;
}
private static string GetMD5HashFromFile(string fileName)//该方法用来进行获取文件的md5加密 用来比对 client文件是否被篡改
{
try
{
FileStream file = new FileStream(fileName, FileMode.Open);
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] retVal = md5.ComputeHash(file);
file.Close();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < retVal.Length; i++)
{
// sb.Append(retVal[i].ToString("x2"));//每两位转换为16进制、
sb.Append(retVal[i].ToString("X2"));//这儿必须是x2 才行 不然转换的时候 0Xeasy转为X 0 会被去掉、
}
return sb.ToString();
}
catch (Exception ex)
{
throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message);
}
}