手机
当前位置:查字典教程网 >编程开发 >C#教程 >计算字符串和文件MD5值的小例子
计算字符串和文件MD5值的小例子
摘要:复制代码代码如下://计算字符串的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;

}

【计算字符串和文件MD5值的小例子】相关文章:

c#对字符串操作的技巧小结

C#简单获取时间差的小例子

c#固定长度的随机字符串例子

c#使用linq技术创建xml文件的小例子

用 C# 编写一个停放在任务栏上的图标程序

C#实现写入与读出文本文件的实例代码

将DLL放入到资源中,运行时自动加载的小例子

深入C#字符串和享元(Flyweight)模式的使用分析

c# 获取网页中指定的字符串信息的实例代码

sort page 排序和分页的小例子

精品推荐
分类导航