地铁二期项目正式开始

DESEncrypt.cs 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. using System.Configuration;
  5. namespace YTSoft.Common
  6. {
  7. /// <summary>
  8. /// DES加密/解密类。
  9. /// </summary>
  10. public class DESEncrypt
  11. {
  12. #region Key
  13. private static string DESKey = ConfigurationManager.AppSettings["DESKey"].ToString();
  14. #endregion
  15. #region ========加密========
  16. /// <summary>
  17. /// 加密
  18. /// </summary>
  19. /// <param name="Text"></param>
  20. /// <returns></returns>
  21. public static string Encrypt(string Text)
  22. {
  23. if (string.IsNullOrEmpty(Text))
  24. return string.Empty;
  25. return Encrypt(Text, DESKey);
  26. }
  27. /// <summary>
  28. /// 加密数据
  29. /// </summary>
  30. /// <param name="Text"></param>
  31. /// <param name="sKey"></param>
  32. /// <returns></returns>
  33. public static string Encrypt(string Text, string sKey)
  34. {
  35. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  36. byte[] inputByteArray;
  37. inputByteArray = Encoding.Default.GetBytes(Text);
  38. des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  39. des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
  40. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  41. CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
  42. cs.Write(inputByteArray, 0, inputByteArray.Length);
  43. cs.FlushFinalBlock();
  44. StringBuilder ret = new StringBuilder();
  45. foreach (byte b in ms.ToArray())
  46. {
  47. ret.AppendFormat("{0:X2}", b);
  48. }
  49. return ret.ToString();
  50. }
  51. #endregion
  52. #region ========解密========
  53. /// <summary>
  54. /// 解密
  55. /// </summary>
  56. /// <param name="Text"></param>
  57. /// <returns></returns>
  58. public static string Decrypt(string Text)
  59. {
  60. if (string.IsNullOrWhiteSpace(Text))
  61. return string.Empty;
  62. return Decrypt(Text, DESKey);
  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. }