/infrastructure/iPow.Infrastructure.Crosscutting.Function/EncryptionHelper.cs

https://bitbucket.org/JPomichael/miaow · C# · 84 lines · 53 code · 9 blank · 22 comment · 1 complexity · 16f149e44217dcc191a57195d17d4bbf MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Security.Cryptography;
  5. using System.Web;
  6. using System.IO;
  7. namespace iPow.Infrastructure.Crosscutting.Function
  8. {
  9. /// <summary>
  10. /// DESEnCode DES密码工具集
  11. /// Copyright (C) 2008-2010 深圳互动力科技有限公司
  12. /// All rights reserved
  13. /// </summary>
  14. public class EncryptionHelper
  15. {
  16. #region DESEnCode DES加密
  17. /// <summary>
  18. /// DES加密函数
  19. /// </summary>
  20. /// <param name="pToEncrypt">要加密的字符串</param>
  21. /// <param name="sKey">加密密钥</param>
  22. /// <returns>返回加密字符串</returns>
  23. public static string DESEnCode(string pToEncrypt, string sKey)
  24. {
  25. pToEncrypt = HttpContext.Current.Server.UrlEncode(pToEncrypt);
  26. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  27. byte[] inputByteArray = Encoding.GetEncoding("UTF-8").GetBytes(pToEncrypt);
  28. //建立加密对象的密钥和偏移量
  29. //原文使用ASCIIEncoding.ASCII方法的GetBytes方法
  30. //使得输入密码必须输入英文文本
  31. des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  32. des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  33. MemoryStream ms = new MemoryStream();
  34. CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
  35. cs.Write(inputByteArray, 0, inputByteArray.Length);
  36. cs.FlushFinalBlock();
  37. StringBuilder ret = new StringBuilder();
  38. foreach (byte b in ms.ToArray())
  39. {
  40. ret.AppendFormat("{0:X2}", b);
  41. }
  42. ret.ToString();
  43. return ret.ToString();
  44. }
  45. #endregion
  46. #region DESDeCode DES解密
  47. /// <summary>
  48. /// DES解密函数
  49. /// </summary>
  50. /// <param name="pToDecrypt">需要被解密的字符串</param>
  51. /// <param name="sKey">解密密钥</param>
  52. /// <returns>返回解密字符串</returns>
  53. public static string DESDeCode(string pToDecrypt, string sKey)
  54. {
  55. // HttpContext.Current.Response.Write(pToDecrypt + "<br>" + sKey);
  56. // HttpContext.Current.Response.End();
  57. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  58. byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
  59. for (int x = 0; x < pToDecrypt.Length / 2; x++)
  60. {
  61. int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
  62. inputByteArray[x] = (byte)i;
  63. }
  64. des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  65. des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  66. MemoryStream ms = new MemoryStream();
  67. CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
  68. cs.Write(inputByteArray, 0, inputByteArray.Length);
  69. cs.FlushFinalBlock();
  70. StringBuilder ret = new StringBuilder();
  71. return HttpContext.Current.Server.UrlDecode(System.Text.Encoding.Default.GetString(ms.ToArray()));
  72. }
  73. #endregion
  74. }
  75. }