标签:
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; }
标签:
原文地址:http://www.cnblogs.com/baixingqiang/p/5492232.html