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.

88 rivejä
3.3 KiB

3 vuotta sitten
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. /// <summary>
  8. ///DESEncrypt 的摘要说明
  9. /// </summary>
  10. namespace Basic.Tools
  11. {
  12. public class DESEncrypt
  13. {
  14. /// <summary>
  15. /// 加密
  16. /// </summary>
  17. /// <param name="Text"></param>
  18. /// <returns></returns>
  19. public static string Encrypt(string Text)
  20. {
  21. return Encrypt(Text, "str_key");
  22. }
  23. /// <summary>
  24. /// 加密数据
  25. /// </summary>
  26. /// <param name="Text"></param>
  27. /// <param name="sKey"></param>
  28. /// <returns></returns>
  29. public static string Encrypt(string Text, string sKey)
  30. {
  31. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  32. byte[] inputByteArray;
  33. inputByteArray = Encoding.Default.GetBytes(Text);
  34. des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  35. des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  36. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  37. CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
  38. cs.Write(inputByteArray, 0, inputByteArray.Length);
  39. cs.FlushFinalBlock();
  40. StringBuilder ret = new StringBuilder();
  41. foreach (byte b in ms.ToArray())
  42. {
  43. ret.AppendFormat("{0:X2}", b);
  44. }
  45. return ret.ToString();
  46. }
  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, "str_key");
  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. }
  83. }