标签:
1.字符串加密
public string GetMD5String(string str) { MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); //md5 = new MD5CryptoServiceProvider(); byte[] data = System.Text.Encoding.Default.GetBytes(str);// 返回结果:一个字节数组,包含对指定的字符集进行编码的结果。 byte[] md5data = md5.ComputeHash(data);//计算指定字节数组的哈希值。 //md5.Clear(); StringBuilder builder = new StringBuilder(); for (int i = 0; i < md5data.Length; i++) { //Console.WriteLine(md5data[i].ToString()); builder.Append(md5data[i].ToString("X2")); } //返回加密后的字符串 return builder.ToString(); }
2.文件加密
public 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/zhangzongle/p/5349675.html