计算字符串和文件MD5值的小例子_C#教程-查字典教程网
计算字符串和文件MD5值的小例子
计算字符串和文件MD5值的小例子
发布时间:2016-12-28 来源:查字典编辑
摘要:复制代码代码如下://计算字符串的MD5值publicstringGetMD5(stringsDataIn){MD5CryptoServic...

复制代码 代码如下:

//计算字符串的MD5值

public string GetMD5(string sDataIn)

{

MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

byte[] bytValue, bytHash;

bytValue = System.Text.Encoding.UTF8.GetBytes(sDataIn);

bytHash = md5.ComputeHash(bytValue);

md5.Clear();

string sTemp = "";

for (int i = 0; i < bytHash.Length; i++)

{

sTemp += bytHash[i].ToString("X").PadLeft(2, '0');

}

return sTemp.ToLower();

}

//计算文件的MD5值

public string MD5Value(String filepath)

{

MD5 md5 = new MD5CryptoServiceProvider();

byte[] md5ch;

using (FileStream fs = File.OpenRead(filepath))

{

md5ch = md5.ComputeHash(fs);

}

md5.Clear();

string strMd5 = "";

for (int i = 0; i < md5ch.Length - 1; i++)

{

strMd5 += md5ch[i].ToString("x").PadLeft(2, '0');

}

return strMd5;

}

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新C#教程学习
    热门C#教程学习
    编程开发子分类