您的位置:首页 > 编程语言 > C#

c# 计算字符串和文件的MD5值的方法

2015-05-07 05:52 288 查看

快速使用Romanysoft LAB的技术实现 HTML 开发Mac OS App,并销售到苹果应用商店中。

《HTML开发Mac OS App 视频教程》

土豆网同步更新:http://www.tudou.com/plcover/VHNh6ZopQ4E/

百度网盘同步:http://pan.baidu.com/s/1jG1Q58M

分享 [中文纪录片]互联网时代 http://pan.baidu.com/s/1qWkJfcS

官方QQ群:(申请加入,说是我推荐的

App实践出真知 434558944



App学习交流 452180823





Step1:引入库

using System.Security.Cryptography;
using System.IO;


Step2: 计算字符串的Md5值

static public string GetMD5WithString(string sDataIn)
{
string str = "";
byte[] data = Encoding.GetEncoding("utf-8").GetBytes(str);
MD5 md5 = new MD5CryptoServiceProvider();
byte[] bytes = md5.ComputeHash(data);
for (int i = 0; i < bytes.Length; i++)
{
str += bytes[i].ToString("x2");
}
return str;
}


Step3:计算文件的Md5值

static public string GetMD5WithFilePath(string filePath)
{
FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] hash_byte = md5.ComputeHash(file);
string str = System.BitConverter.ToString(hash_byte);
str = str.Replace("-", "");
return str;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: