MD5Util.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace ProjectBase.Data.Encrypt
  8. {
  9. /// <summary>
  10. /// MD5各种长度加密字符、验证MD5等操作辅助类
  11. /// </summary>
  12. public class MD5Util
  13. {
  14. /// <summary>
  15. /// 构造函数
  16. /// </summary>
  17. public MD5Util()
  18. {
  19. }
  20. /// <summary>
  21. /// 获得32位的MD5加密
  22. /// </summary>
  23. public static string GetMD5_32(string input)
  24. {
  25. System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
  26. byte[] data = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(input));
  27. StringBuilder sb = new StringBuilder();
  28. for (int i = 0; i < data.Length; i++)
  29. {
  30. sb.Append(data[i].ToString("x2"));
  31. }
  32. return sb.ToString();
  33. }
  34. /// <summary>
  35. /// 获得16位的MD5加密
  36. /// </summary>
  37. public static string GetMD5_16(string input)
  38. {
  39. return GetMD5_32(input).Substring(8, 16);
  40. }
  41. /// <summary>
  42. /// 获得8位的MD5加密
  43. /// </summary>
  44. public static string GetMD5_8(string input)
  45. {
  46. return GetMD5_32(input).Substring(8, 8);
  47. }
  48. /// <summary>
  49. /// 获得4位的MD5加密
  50. /// </summary>
  51. public static string GetMD5_4(string input)
  52. {
  53. return GetMD5_32(input).Substring(8, 4);
  54. }
  55. /// <summary>
  56. /// 添加MD5的前缀,便于检查有无篡改
  57. /// </summary>
  58. public static string AddMD5Profix(string input)
  59. {
  60. return GetMD5_4(input) + input;
  61. }
  62. /// <summary>
  63. /// 移除MD5的前缀
  64. /// </summary>
  65. public static string RemoveMD5Profix(string input)
  66. {
  67. return input.Substring(4);
  68. }
  69. /// <summary>
  70. /// 验证MD5前缀处理的字符串有无被篡改
  71. /// </summary>
  72. public static bool ValidateValue(string input)
  73. {
  74. bool res = false;
  75. if (input.Length >= 4)
  76. {
  77. string tmp = input.Substring(4);
  78. if (input.Substring(0, 4) == GetMD5_4(tmp))
  79. {
  80. res = true;
  81. }
  82. }
  83. return res;
  84. }
  85. #region MD5签名验证
  86. /// <summary>
  87. /// 对给定文件路径的文件加上标签
  88. /// </summary>
  89. /// <param name="path">要加密的文件的路径</param>
  90. /// <returns>标签的值</returns>
  91. public static bool AddMD5(string path)
  92. {
  93. bool IsNeed = true;
  94. if (CheckMD5(path)) //已进行MD5处理
  95. IsNeed = false;
  96. try
  97. {
  98. FileStream fsread = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
  99. byte[] md5File = new byte[fsread.Length];
  100. fsread.Read(md5File, 0, (int)fsread.Length); // 将文件流读取到Buffer中
  101. fsread.Close();
  102. if (IsNeed)
  103. {
  104. string result = MD5Buffer(md5File, 0, md5File.Length); // 对Buffer中的字节内容算MD5
  105. byte[] md5 = System.Text.Encoding.ASCII.GetBytes(result); // 将字符串转换成字节数组以便写人到文件中
  106. FileStream fsWrite = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
  107. fsWrite.Write(md5File, 0, md5File.Length); // 将文件,MD5值 重新写入到文件中。
  108. fsWrite.Write(md5, 0, md5.Length);
  109. fsWrite.Close();
  110. }
  111. else
  112. {
  113. FileStream fsWrite = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
  114. fsWrite.Write(md5File, 0, md5File.Length);
  115. fsWrite.Close();
  116. }
  117. }
  118. catch
  119. {
  120. return false;
  121. }
  122. return true;
  123. }
  124. /// <summary>
  125. /// 对给定路径的文件进行验证,如果一致返回True,否则返回False
  126. /// </summary>
  127. /// <param name="path"></param>
  128. /// <returns>是否加了标签或是否标签值与内容值一致</returns>
  129. public static bool CheckMD5(string path)
  130. {
  131. try
  132. {
  133. FileStream get_file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
  134. byte[] md5File = new byte[get_file.Length]; // 读入文件
  135. get_file.Read(md5File, 0, (int)get_file.Length);
  136. get_file.Close();
  137. string result = MD5Buffer(md5File, 0, md5File.Length - 32); // 对文件除最后32位以外的字节计算MD5,这个32是因为标签位为32位。
  138. string md5 = System.Text.Encoding.ASCII.GetString(md5File, md5File.Length - 32, 32); //读取文件最后32位,其中保存的就是MD5值
  139. return result == md5;
  140. }
  141. catch
  142. {
  143. return false;
  144. }
  145. }
  146. /// <summary>
  147. /// 计算文件的MD5值
  148. /// </summary>
  149. /// <param name="MD5File">MD5签名文件字符数组</param>
  150. /// <param name="index">计算起始位置</param>
  151. /// <param name="count">计算终止位置</param>
  152. /// <returns>计算结果</returns>
  153. private static string MD5Buffer(byte[] MD5File, int index, int count)
  154. {
  155. System.Security.Cryptography.MD5CryptoServiceProvider get_md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
  156. byte[] hash_byte = get_md5.ComputeHash(MD5File, index, count);
  157. string result = System.BitConverter.ToString(hash_byte);
  158. result = result.Replace("-", "");
  159. return result;
  160. }
  161. #endregion
  162. private void Test()
  163. {
  164. string o = "i love u";
  165. o = AddMD5Profix(o);
  166. //o += " ";
  167. Console.WriteLine(o);
  168. Console.WriteLine(ValidateValue(o));
  169. o = RemoveMD5Profix(o);
  170. Console.WriteLine(o);
  171. }
  172. }
  173. }