DESEncrypt.cs 2.8 KB

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