MD5加密

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; }

 

posted @ 2016-05-14 11:41  白兴强  阅读(190)  评论(0编辑  收藏  举报