纽威
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.5 KiB

3 years ago
  1. using System;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. namespace NFine.Code
  5. {
  6. /// <summary>
  7. /// DES加密、解密帮助类
  8. /// </summary>
  9. public class DESEncrypt
  10. {
  11. private static string DESKey = "nfine_desencrypt_2016";
  12. #region ========加密========
  13. /// <summary>
  14. /// 加密
  15. /// </summary>
  16. /// <param name="Text"></param>
  17. /// <returns></returns>
  18. public static string Encrypt(string Text)
  19. {
  20. return Encrypt(Text, DESKey);
  21. }
  22. /// <summary>
  23. /// 加密数据
  24. /// </summary>
  25. /// <param name="Text"></param>
  26. /// <param name="sKey"></param>
  27. /// <returns></returns>
  28. public static string Encrypt(string Text, string sKey)
  29. {
  30. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  31. byte[] inputByteArray;
  32. inputByteArray = Encoding.Default.GetBytes(Text);
  33. des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  34. des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  35. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  36. CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
  37. cs.Write(inputByteArray, 0, inputByteArray.Length);
  38. cs.FlushFinalBlock();
  39. StringBuilder ret = new StringBuilder();
  40. foreach (byte b in ms.ToArray())
  41. {
  42. ret.AppendFormat("{0:X2}", b);
  43. }
  44. return ret.ToString();
  45. }
  46. #endregion
  47. #region ========解密========
  48. /// <summary>
  49. /// 解密
  50. /// </summary>
  51. /// <param name="Text"></param>
  52. /// <returns></returns>
  53. public static string Decrypt(string Text)
  54. {
  55. if (!string.IsNullOrEmpty(Text))
  56. {
  57. return Decrypt(Text, DESKey);
  58. }
  59. else
  60. {
  61. return "";
  62. }
  63. }
  64. /// <summary>
  65. /// 解密数据
  66. /// </summary>
  67. /// <param name="Text"></param>
  68. /// <param name="sKey"></param>
  69. /// <returns></returns>
  70. public static string Decrypt(string Text, string sKey)
  71. {
  72. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  73. int len;
  74. len = Text.Length / 2;
  75. byte[] inputByteArray = new byte[len];
  76. int x, i;
  77. for (x = 0; x < len; x++)
  78. {
  79. i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
  80. inputByteArray[x] = (byte)i;
  81. }
  82. des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  83. des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  84. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  85. CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
  86. cs.Write(inputByteArray, 0, inputByteArray.Length);
  87. cs.FlushFinalBlock();
  88. return Encoding.Default.GetString(ms.ToArray());
  89. }
  90. #endregion
  91. }
  92. }