/App.Core/Helper/SecretHelper.cs

http://colvinliu.googlecode.com/ · C# · 96 lines · 68 code · 5 blank · 23 comment · 1 complexity · 246307a39c9c2e6c8fcda7e339acb349 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web.Security;
  6. using System.Collections;
  7. using System.Security.Cryptography;
  8. using System.IO;
  9. namespace Job.Core
  10. {
  11. /// <summary>
  12. /// ???
  13. /// </summary>
  14. public class SecretHelper
  15. {
  16. public static string ToMd5(string strPwd)
  17. {
  18. return FormsAuthentication.HashPasswordForStoringInConfigFile(strPwd, "MD5");
  19. }
  20. /// <summary>
  21. /// ????
  22. /// </summary>
  23. /// <param name="pToEncrypt">???????</param>
  24. /// <param name="sKey">??</param>
  25. /// <returns>???????</returns>
  26. public static string Encrypt(string pToEncrypt, string sKey)
  27. {
  28. try
  29. {
  30. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  31. //??????byte???
  32. //?????UTF8??????Unicode??????
  33. byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
  34. //?????????????
  35. //??????????????
  36. des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  37. des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  38. MemoryStream ms = new MemoryStream();
  39. CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
  40. cs.Write(inputByteArray, 0, inputByteArray.Length);
  41. cs.FlushFinalBlock();
  42. StringBuilder ret = new StringBuilder();
  43. foreach (byte b in ms.ToArray())
  44. {
  45. ret.AppendFormat("{0:X2}", b);
  46. }
  47. ret.ToString();
  48. return ret.ToString();
  49. }
  50. catch
  51. {
  52. //JS.Alert("??????????????" + ex.Message.Replace("\r\n", "").Replace("'", ""));
  53. }
  54. return "";
  55. }
  56. /// <summary>
  57. /// ????
  58. /// </summary>
  59. /// <param name="pToDecrypt">????????</param>
  60. /// <param name="sKey">??</param>
  61. /// <returns>???????</returns>
  62. public static string Decrypt(string pToDecrypt, string sKey)
  63. {
  64. try
  65. {
  66. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  67. byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
  68. for (int x = 0; x < pToDecrypt.Length / 2; x++)
  69. {
  70. int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
  71. inputByteArray[x] = (byte)i;
  72. }
  73. //???????????????????????
  74. des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  75. des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  76. MemoryStream ms = new MemoryStream();
  77. CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
  78. cs.Write(inputByteArray, 0, inputByteArray.Length);
  79. cs.FlushFinalBlock();
  80. //??StringBuild???CreateDecrypt??????????????????????
  81. StringBuilder ret = new StringBuilder();
  82. return System.Text.Encoding.Default.GetString(ms.ToArray());
  83. }
  84. catch
  85. {
  86. //JS.Alert("??????????????" + ex.Message.Replace("\r\n", "").Replace("'", ""));
  87. }
  88. return "";
  89. }
  90. }
  91. }