码迷,mamicode.com
首页 > 其他好文 > 详细

MD5加密

时间:2016-05-14 12:42:39      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

MD5加密字符串


引入命名空间
using System.Security.Cryptography;

方式一:
public string GetMD5String(string pwd) { MD5CryptoServiceProvider md5=new MD5CryptoServiceProvide(); byte[] data=System.Text.Encoding.ASCII.GetBytes(pwd); byte[] md5data=md5.ComputeHash(data); StringBuilder builder=new StringBUilder(); for(int i=0;i<md5data.Length-1;i++) { builder.Append(md5data[i].ToString("X2")); } return builder; } 方式二: public string GetMd5(string msg) { string cryptStr = ""; MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); byte[] bytes = Encoding.UTF8.GetBytes(msg); byte[] cryptBytes = md5.ComputeHash(bytes); for (int i = 0; i < cryptBytes.Length; i++) { cryptStr += cryptBytes[i].ToString("X2"); } return cryptStr; }

MD5加密文件


引入命名空间
using System.Security.Cryptography;

string
GetFileMd5(string path) { string ctyptStr = ""; byte[] cryptBytes; using (FileStream fs = new FileStream(path,FileMode.Open)) { MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); cryptBytes = md5.ComputeHash(fs); } for (int i = 0; i < cryptBytes.Length; i++) { ctyptStr += cryptBytes[i].ToString("X2"); } return ctyptStr; }

 

MD5加密

标签:

原文地址:http://www.cnblogs.com/baixingqiang/p/5492232.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!