/DaleCloud.Code/Security/DESEncrypt.cs

https://github.com/wuyuande/DaleCloudNetFramework · C# · 219 lines · 146 code · 15 blank · 58 comment · 5 complexity · 5e4892e37218805a868abea2685bf0eb MD5 · raw file

  1. /*******************************************************************************
  2. * Copyright © 2016 NFine.Framework 版权所有
  3. * Author: DaleCloud
  4. * Description: DaleCloud快速开发平台
  5. * Website:
  6. *********************************************************************************/
  7. using System;
  8. using System.Security.Cryptography;
  9. using System.Text;
  10. namespace DaleCloud.Code
  11. {
  12. /// <summary>
  13. /// DES加密、解密帮助类
  14. /// </summary>
  15. public class DESEncrypt
  16. {
  17. private static string DESKey = "dalecloud_desencrypt_2019";
  18. #region ========加密========
  19. /// <summary>
  20. /// 加密
  21. /// </summary>
  22. /// <param name="Text"></param>
  23. /// <returns></returns>
  24. public static string Encrypt(string Text)
  25. {
  26. return Encrypt(Text, DESKey);
  27. }
  28. /// <summary>
  29. /// 加密数据,用Web.Security的Hash方式加密
  30. /// </summary>
  31. /// <param name="Text"></param>
  32. /// <param name="sKey"></param>
  33. /// <returns></returns>
  34. public static string Encrypt(string Text, string sKey)
  35. {
  36. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  37. byte[] inputByteArray;
  38. inputByteArray = Encoding.Default.GetBytes(Text);
  39. des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  40. des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  41. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  42. CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
  43. cs.Write(inputByteArray, 0, inputByteArray.Length);
  44. cs.FlushFinalBlock();
  45. StringBuilder ret = new StringBuilder();
  46. foreach (byte b in ms.ToArray())
  47. {
  48. ret.AppendFormat("{0:X2}", b);
  49. }
  50. return ret.ToString();
  51. }
  52. /// <summary>
  53. /// 加密数据, 用Security.MD5而非Web.Security的Hash方式加密
  54. /// </summary>
  55. /// <param name="Text"></param>
  56. /// <returns></returns>
  57. public static string Encrypt2(string Text)
  58. {
  59. return Encrypt2(Text, DESKey);
  60. }
  61. /// <summary>
  62. /// 加密数据, 用Security.MD5而非Web.Security的Hash方式加密
  63. /// </summary>
  64. /// <param name="Text"></param>
  65. /// <param name="sKey"></param>
  66. /// <returns></returns>
  67. public static string Encrypt2(string Text, string sKey)
  68. {
  69. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  70. byte[] inputByteArray;
  71. inputByteArray = Encoding.Default.GetBytes(Text);
  72. sKey = MD5(sKey).Substring(0, 8);
  73. des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  74. des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  75. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  76. CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
  77. cs.Write(inputByteArray, 0, inputByteArray.Length);
  78. cs.FlushFinalBlock();
  79. StringBuilder ret = new StringBuilder();
  80. foreach (byte b in ms.ToArray())
  81. {
  82. ret.AppendFormat("{0:X2}", b);
  83. }
  84. return ret.ToString();
  85. }
  86. #endregion
  87. #region ========解密========
  88. /// <summary>
  89. /// 解密
  90. /// </summary>
  91. /// <param name="Text"></param>
  92. /// <returns></returns>
  93. public static string Decrypt(string Text)
  94. {
  95. if (!string.IsNullOrEmpty(Text))
  96. {
  97. return Decrypt(Text, DESKey);
  98. }
  99. else
  100. {
  101. return "";
  102. }
  103. }
  104. /// <summary>
  105. /// 解密数据,用Web.Security的Hash方式加密
  106. /// </summary>
  107. /// <param name="Text"></param>
  108. /// <param name="sKey"></param>
  109. /// <returns></returns>
  110. public static string Decrypt(string Text, string sKey)
  111. {
  112. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  113. int len;
  114. len = Text.Length / 2;
  115. byte[] inputByteArray = new byte[len];
  116. int x, i;
  117. for (x = 0; x < len; x++)
  118. {
  119. i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
  120. inputByteArray[x] = (byte)i;
  121. }
  122. des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  123. des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  124. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  125. CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
  126. cs.Write(inputByteArray, 0, inputByteArray.Length);
  127. cs.FlushFinalBlock();
  128. return Encoding.Default.GetString(ms.ToArray());
  129. }
  130. /// <summary>
  131. /// 解密数据,用Security.MD5而非Web.Security的Hash方式加密
  132. /// </summary>
  133. /// <param name="Text"></param>
  134. /// <returns></returns>
  135. public static string Decrypt2(string Text)
  136. {
  137. if (!string.IsNullOrEmpty(Text))
  138. {
  139. return Decrypt2(Text, DESKey);
  140. }
  141. else
  142. {
  143. return "";
  144. }
  145. }
  146. /// <summary>
  147. /// 解密数据,用Security.MD5而非Web.Security的Hash方式加密
  148. /// </summary>
  149. /// <param name="Text"></param>
  150. /// <param name="sKey"></param>
  151. /// <returns></returns>
  152. public static string Decrypt2(string Text, string sKey)
  153. {
  154. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  155. int len;
  156. len = Text.Length / 2;
  157. byte[] inputByteArray = new byte[len];
  158. int x, i;
  159. for (x = 0; x < len; x++)
  160. {
  161. i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
  162. inputByteArray[x] = (byte)i;
  163. }
  164. sKey = MD5(sKey).Substring(0, 8);
  165. des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  166. des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  167. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  168. CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
  169. cs.Write(inputByteArray, 0, inputByteArray.Length);
  170. cs.FlushFinalBlock();
  171. return Encoding.Default.GetString(ms.ToArray());
  172. }
  173. #endregion
  174. public static string MD5(string pwd)
  175. {
  176. MD5 md5 = new MD5CryptoServiceProvider();
  177. byte[] data = System.Text.Encoding.Default.GetBytes(pwd);
  178. byte[] md5data = md5.ComputeHash(data);
  179. md5.Clear();
  180. string str = "";
  181. for (int i = 0; i < md5data.Length; i++)
  182. {
  183. str += md5data[i].ToString("x").PadLeft(2, '0');
  184. }
  185. return str;
  186. }
  187. /// <summary>
  188. /// 基于Sha1的自定义加密字符串方法:输入一个字符串,返回一个由40个字符组成的十六进制的哈希散列(字符串)。
  189. /// </summary>
  190. /// <param name="str">要加密的字符串</param>
  191. /// <returns>加密后的十六进制的哈希散列(字符串)</returns>
  192. public static string Sha1(string str)
  193. {
  194. var buffer = Encoding.UTF8.GetBytes(str);
  195. var data = SHA1.Create().ComputeHash(buffer);
  196. var sb = new StringBuilder();
  197. foreach (var t in data)
  198. {
  199. sb.Append(t.ToString("X2"));
  200. }
  201. return sb.ToString();
  202. }
  203. }
  204. }