查看: 2150|回复: 0

[ASP.NET教程] MD5密码加密

发表于 2018-4-25 08:00:02
  1. using System;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. namespace DimoNetwork.Common.DEncrypt
  5. {
  6. public enum MD5ResultMode : byte
  7. {
  8. Strong = 0,
  9. Weak = 1
  10. }
  11. /// <summary>
  12. /// 在应用程序中定义用于单向加密文本的方法
  13. /// </summary>
  14. public class TextEncrypt
  15. {
  16. private TextEncrypt()
  17. {
  18. }
  19. #region ========加密========
  20. /// <summary>
  21. /// 加密
  22. /// </summary>
  23. /// <param name="Text"></param>
  24. /// <returns></returns>
  25. public static string Encrypt(string Text)
  26. {
  27. return Encrypt(Text, "DimoNet");
  28. }
  29. /// <summary>
  30. /// 加密数据
  31. /// </summary>
  32. /// <param name="Text"></param>
  33. /// <param name="sKey"></param>
  34. /// <returns></returns>
  35. public static string Encrypt(string Text, string sKey)
  36. {
  37. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  38. byte[] inputByteArray;
  39. inputByteArray = Encoding.Default.GetBytes(Text);
  40. des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  41. des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  42. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  43. CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
  44. cs.Write(inputByteArray, 0, inputByteArray.Length);
  45. cs.FlushFinalBlock();
  46. StringBuilder ret = new StringBuilder();
  47. foreach (byte b in ms.ToArray())
  48. {
  49. ret.AppendFormat("{0:X2}", b);
  50. }
  51. return ret.ToString();
  52. }
  53. /// <summary>
  54. /// MD5 加密
  55. /// </summary>
  56. /// <param name="password">要加密的字符串</param>
  57. /// <returns></returns>
  58. public static string MD5EncryptPassword(string password, int? length = null)
  59. {
  60. if (password == null)
  61. {
  62. throw new ArgumentNullException("password");
  63. }
  64. return MD5EncryptPassword(password, MD5ResultMode.Strong, length);
  65. }
  66. /// <summary>
  67. /// MD5 加密
  68. /// </summary>
  69. /// <param name="password">要加密的字符串</param>
  70. /// <param name="mode">加密强度</param>
  71. /// <returns></returns>
  72. public static string MD5EncryptPassword(string password, MD5ResultMode mode, int? length = null)
  73. {
  74. if (password == null)
  75. {
  76. throw new ArgumentNullException("password");
  77. }
  78. MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
  79. string str = BitConverter.ToString(provider.ComputeHash(Encoding.UTF8.GetBytes(password)));
  80. if (length != null && length == 16)
  81. {
  82. str = BitConverter.ToString(provider.ComputeHash(Encoding.UTF8.GetBytes(password)), 4, 8);
  83. }
  84. provider.Clear();
  85. if (mode != MD5ResultMode.Strong)
  86. {
  87. return str.Replace("-", null).Substring(8, 0x10);
  88. }
  89. return str.Replace("-", null);
  90. }
  91. #endregion
  92. #region ========解密========
  93. /// <summary>
  94. /// 解密
  95. /// </summary>
  96. /// <param name="Text"></param>
  97. /// <returns></returns>
  98. public static string Decrypt(string Text)
  99. {
  100. return Decrypt(Text, "DimoNet");
  101. }
  102. /// <summary>
  103. /// 解密数据
  104. /// </summary>
  105. /// <param name="Text"></param>
  106. /// <param name="sKey"></param>
  107. /// <returns></returns>
  108. public static string Decrypt(string Text, string sKey)
  109. {
  110. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  111. int len;
  112. len = Text.Length / 2;
  113. byte[] inputByteArray = new byte[len];
  114. int x, i;
  115. for (x = 0; x < len; x++)
  116. {
  117. // i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
  118. i = System.Convert.ToInt32(Text.Substring(x * 2, 2), 16);
  119. inputByteArray[x] = (byte)i;
  120. }
  121. des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  122. des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  123. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  124. CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
  125. cs.Write(inputByteArray, 0, inputByteArray.Length);
  126. cs.FlushFinalBlock();
  127. return Encoding.Default.GetString(ms.ToArray());
  128. }
  129. #endregion
  130. /// <summary>
  131. /// Base64 解码
  132. /// </summary>
  133. /// <param name="message"></param>
  134. /// <returns></returns>
  135. public static string Base64Decode(string message)
  136. {
  137. byte[] bytes = Convert.FromBase64String(message);
  138. return Encoding.UTF8.GetString(bytes);
  139. }
  140. /// <summary>
  141. /// Base64 编码
  142. /// </summary>
  143. /// <param name="message"></param>
  144. /// <returns></returns>
  145. public static string Base64Encode(string message)
  146. {
  147. return Convert.ToBase64String(Encoding.UTF8.GetBytes(message));
  148. }
  149. /// <summary>
  150. /// DSA 加密
  151. /// </summary>
  152. /// <param name="password">要加密的字符串</param>
  153. /// <returns></returns>
  154. public static string DSAEncryptPassword(string password)
  155. {
  156. if (password == null)
  157. {
  158. throw new ArgumentNullException("password");
  159. }
  160. DSACryptoServiceProvider provider = new DSACryptoServiceProvider();
  161. string str = BitConverter.ToString(provider.SignData(Encoding.UTF8.GetBytes(password)));
  162. provider.Clear();
  163. return str.Replace("-", null);
  164. }
  165. /// <summary>
  166. /// MD5 加密
  167. /// </summary>
  168. /// <param name="password">要加密的字符串</param>
  169. /// <returns></returns>
  170. public static string EncryptPassword(string password)
  171. {
  172. if (password == null)
  173. {
  174. throw new ArgumentNullException("password");
  175. }
  176. return MD5EncryptPassword(password);
  177. }
  178. /// <summary>
  179. /// MD5 加密
  180. /// </summary>
  181. /// <param name="password">要加密的字符串</param>
  182. /// <returns></returns>
  183. public static string MD5EncryptPassword(string password)
  184. {
  185. if (password == null)
  186. {
  187. throw new ArgumentNullException("password");
  188. }
  189. return MD5EncryptPassword(password, MD5ResultMode.Strong);
  190. }
  191. /// <summary>
  192. /// MD5 加密
  193. /// </summary>
  194. /// <param name="password">要加密的字符串</param>
  195. /// <param name="mode">加密强度</param>
  196. /// <returns></returns>
  197. public static string MD5EncryptPassword(string password, MD5ResultMode mode)
  198. {
  199. if (password == null)
  200. {
  201. throw new ArgumentNullException("password");
  202. }
  203. MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
  204. string str = BitConverter.ToString(provider.ComputeHash(Encoding.UTF8.GetBytes(password)));
  205. provider.Clear();
  206. if (mode != MD5ResultMode.Strong)
  207. {
  208. return str.Replace("-", null).Substring(8, 0x10);
  209. }
  210. return str.Replace("-", null);
  211. }
  212. /// <summary>
  213. /// SHA1 加密
  214. /// </summary>
  215. /// <param name="password">要加密的字符串</param>
  216. /// <returns></returns>
  217. public static string SHA1EncryptPassword(string password)
  218. {
  219. if (password == null)
  220. {
  221. throw new ArgumentNullException("password");
  222. }
  223. SHA1CryptoServiceProvider provider = new SHA1CryptoServiceProvider();
  224. string str = BitConverter.ToString(provider.ComputeHash(Encoding.UTF8.GetBytes(password)));
  225. provider.Clear();
  226. return str.Replace("-", null);
  227. }
  228. /// <summary>
  229. /// SHA256 加密
  230. /// </summary>
  231. /// <param name="password">要加密的字符串</param>
  232. /// <returns></returns>
  233. public static string SHA256(string password)
  234. {
  235. byte[] bytes = Encoding.UTF8.GetBytes(password);
  236. SHA256Managed managed = new SHA256Managed();
  237. return Convert.ToBase64String(managed.ComputeHash(bytes));
  238. }
  239. }
  240. }
复制代码



回复

使用道具 举报