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.

94 lines
3.3 KiB

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