/NBCZ.DBUtility/DESEncrypt.cs

https://github.com/chi8708/NBCZ_Admin · C# · 136 lines · 88 code · 9 blank · 39 comment · 1 complexity · 7dd6325abb4f675613f519253f33a7f1 MD5 · raw file

  1. using System;
  2. using System.Configuration;
  3. using System.IO;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. using System.Web;
  7. namespace NBCZ.DBUtility
  8. {
  9. /// <summary>
  10. /// DES加密/解密类。
  11. /// </summary>
  12. public class DesEncrypt
  13. {
  14. public static string jmkeys = ConfigurationManager.AppSettings["jmkey"];
  15. public static string encryption = "NBCZ948";
  16. /// <summary>
  17. /// 加密
  18. /// </summary>
  19. /// <param name="text"></param>
  20. /// <returns></returns>
  21. public static string Encrypt(string text)
  22. {
  23. return DESEnCode(text, jmkeys);
  24. }
  25. /// <summary>
  26. /// 解密
  27. /// </summary>
  28. /// <param name="text"></param>
  29. /// <returns></returns>
  30. public static string Decrypt(string text)
  31. {
  32. return DESDeCode(text, jmkeys);
  33. }
  34. /// <summary>
  35. /// 加密
  36. /// </summary>
  37. /// <param name="text"></param>
  38. /// <returns></returns>
  39. public static string Ecode(string text)
  40. {
  41. return DESEnCode(text, encryption);
  42. }
  43. /// <summary>
  44. /// 解密
  45. /// </summary>
  46. /// <param name="text"></param>
  47. /// <returns></returns>
  48. public static string Dcode(string text)
  49. {
  50. return DESDeCode(text, encryption);
  51. }
  52. /// <summary>
  53. /// 加密字符串
  54. /// </summary>
  55. /// <param name="pToEncrypt">要加密的字符</param>
  56. /// <param name="sKey">密钥</param>
  57. /// <returns></returns>
  58. public static string DESEnCode(string pToEncrypt, string sKey)
  59. {
  60. try
  61. {
  62. pToEncrypt = System.Web.HttpUtility.UrlEncode(pToEncrypt);
  63. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  64. byte[] inputByteArray = Encoding.GetEncoding("UTF-8").GetBytes(pToEncrypt);
  65. des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  66. des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  67. MemoryStream ms = new MemoryStream();
  68. CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
  69. cs.Write(inputByteArray, 0, inputByteArray.Length);
  70. cs.FlushFinalBlock();
  71. StringBuilder ret = new StringBuilder();
  72. foreach (byte b in ms.ToArray())
  73. {
  74. ret.AppendFormat("{0:X2}", b);
  75. }
  76. ret.ToString();
  77. return ret.ToString();
  78. }
  79. catch
  80. {
  81. return null;
  82. }
  83. }
  84. #region DESDeCode DES解密
  85. /// <summary>
  86. /// 解密字符串
  87. /// </summary>
  88. /// <param name="pToDecrypt">解密字符</param>
  89. /// <param name="sKey">密钥</param>
  90. /// <returns></returns>
  91. public static string DESDeCode(string pToDecrypt, string sKey)
  92. {
  93. try
  94. {
  95. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  96. byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
  97. for (int x = 0; x < pToDecrypt.Length / 2; x++)
  98. {
  99. int i = (System.Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
  100. inputByteArray[x] = (byte)i;
  101. }
  102. des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  103. des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  104. MemoryStream ms = new MemoryStream();
  105. CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
  106. cs.Write(inputByteArray, 0, inputByteArray.Length);
  107. cs.FlushFinalBlock();
  108. StringBuilder ret = new StringBuilder();
  109. return HttpContext.Current.Server.UrlDecode(System.Text.Encoding.Default.GetString(ms.ToArray()));
  110. }
  111. catch
  112. {
  113. return null;
  114. }
  115. }
  116. /// <summary>
  117. /// 获取时间戳
  118. /// </summary>
  119. /// <returns></returns>
  120. public static string GetTimeStamp()
  121. {
  122. TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  123. return Convert.ToInt64(ts.TotalSeconds).ToString();
  124. }
  125. #endregion
  126. }
  127. }