手机
当前位置:查字典教程网 >编程开发 >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#启动EXE文件的方法实例

c#解压文件的实例方法

在c#中把字符串转为变量名并获取变量值的小例子

C#之IP地址和整数互转的小例子

c# 匿名方法的小例子

c# 委托和事件实例学习

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

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

c#简单读取文本的实例方法

精品推荐
分类导航