DESHelper.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. *
  3. * @date 2014-11-01
  4. *
  5. * @author 邓尹平
  6. *
  7. * @par 成员列表类 说明
  8. * 主要负责管理成员 提供搜索\排序和操作的方法
  9. *
  10. * @par 版权声明
  11. * 深圳市多元世纪信息技术有限公司 版权所有
  12. *
  13. * @see 使用此类时参照一些其他类可以写在这里
  14. *
  15. * @todo 该类有待完成的任务 一条条列出 完成一条删除一条
  16. *
  17. * @bug 该类已知的Bug一条条列出 完成一条删除一条
  18. *
  19. */
  20. using System;
  21. using System.Globalization;
  22. using System.Security.Cryptography;
  23. using System.Text;
  24. namespace Muchinfo.PC.Common.Helpers
  25. {
  26. public class DESHelper
  27. {
  28. private static readonly string _key = "MuChInFo";
  29. /// <summary>
  30. /// DES加密
  31. /// </summary>
  32. /// <param name="sourceString">The source string.</param>
  33. /// <returns>System.String.</returns>
  34. public static string Encrypt(string sourceString)
  35. {
  36. try
  37. {
  38. byte[] data = Encoding.UTF8.GetBytes(sourceString);
  39. var des = new DESCryptoServiceProvider
  40. {
  41. Key = Encoding.ASCII.GetBytes(_key),
  42. IV = Encoding.ASCII.GetBytes(_key)
  43. };
  44. ICryptoTransform desencrypt = des.CreateEncryptor();
  45. byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
  46. return BitConverter.ToString(result);
  47. }
  48. catch (Exception e)
  49. {
  50. throw e;
  51. }
  52. }
  53. /// <summary>
  54. /// DES解密
  55. /// </summary>
  56. /// <param name="encryptedString">The encrypted string.</param>
  57. /// <returns>System.String.</returns>
  58. public static string Decrypt(string encryptedString)
  59. {
  60. try
  61. {
  62. string[] sInput = encryptedString.Split("-".ToCharArray());
  63. byte[] data = new byte[sInput.Length];
  64. for (int i = 0; i < sInput.Length; i++)
  65. {
  66. data[i] = byte.Parse(sInput[i], NumberStyles.HexNumber);
  67. }
  68. var DES = new DESCryptoServiceProvider
  69. {
  70. Key = Encoding.ASCII.GetBytes(_key),
  71. IV = Encoding.ASCII.GetBytes(_key)
  72. };
  73. ICryptoTransform desencrypt = DES.CreateDecryptor();
  74. byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
  75. return Encoding.UTF8.GetString(result);
  76. }
  77. catch (Exception e)
  78. {
  79. throw e;
  80. }
  81. }
  82. }
  83. }