/DoubanFM.Core/Encryption.cs

https://github.com/kfstorm/DoubanFM · C# · 99 lines · 70 code · 8 blank · 21 comment · 1 complexity · 26a38e195f12c685dcb8c28a7ef79bdf MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Security.Cryptography;
  6. using System.IO;
  7. namespace DoubanFM.Core
  8. {
  9. /// <summary>
  10. /// 用于字符串加密的类
  11. /// </summary>
  12. static class Encryption
  13. {
  14. /// <summary>
  15. /// 加密
  16. /// </summary>
  17. /// <param name="rs"></param>
  18. /// <returns></returns>
  19. internal static string Encrypt(string rs)
  20. {
  21. byte[] desKey = Encoding.ASCII.GetBytes("DoubanFM");
  22. byte[] desIV = Encoding.ASCII.GetBytes("DoubanFM");
  23. using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
  24. try
  25. {
  26. byte[] inputByteArray = Encoding.Default.GetBytes(rs);
  27. //byte[] inputByteArray=Encoding.Unicode.GetBytes(rs);
  28. des.Key = desKey; // ASCIIEncoding.ASCII.GetBytes(sKey);
  29. des.IV = desIV; //ASCIIEncoding.ASCII.GetBytes(sKey);
  30. using (MemoryStream ms = new MemoryStream())
  31. using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(),
  32. CryptoStreamMode.Write))
  33. {
  34. //Write the byte array into the crypto stream
  35. //(It will end up in the memory stream)
  36. cs.Write(inputByteArray, 0, inputByteArray.Length);
  37. cs.FlushFinalBlock();
  38. //Get the data back from the memory stream, and into a string
  39. StringBuilder ret = new StringBuilder();
  40. foreach (byte b in ms.ToArray())
  41. {
  42. //Format as hex
  43. ret.AppendFormat("{0:X2}", b);
  44. }
  45. ret.ToString();
  46. return ret.ToString();
  47. }
  48. }
  49. catch
  50. {
  51. return rs;
  52. }
  53. }
  54. /// <summary>
  55. /// 解密
  56. /// </summary>
  57. /// <param name="rs"></param>
  58. /// <returns></returns>
  59. internal static string Decrypt(string rs)
  60. {
  61. byte[] desKey = Encoding.ASCII.GetBytes("DoubanFM");
  62. byte[] desIV = Encoding.ASCII.GetBytes("DoubanFM");
  63. using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
  64. try
  65. {
  66. //Put the input string into the byte array
  67. byte[] inputByteArray = new byte[rs.Length / 2];
  68. for (int x = 0; x < rs.Length / 2; x++)
  69. {
  70. int i = (Convert.ToInt32(rs.Substring(x * 2, 2), 16));
  71. inputByteArray[x] = (byte)i;
  72. }
  73. des.Key = desKey; //ASCIIEncoding.ASCII.GetBytes(sKey);
  74. des.IV = desIV; //ASCIIEncoding.ASCII.GetBytes(sKey);
  75. using (MemoryStream ms = new MemoryStream())
  76. using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
  77. {
  78. //Flush the data through the crypto stream into the memory stream
  79. cs.Write(inputByteArray, 0, inputByteArray.Length);
  80. cs.FlushFinalBlock();
  81. //Get the decrypted data back from the memory stream
  82. return System.Text.Encoding.Default.GetString(ms.ToArray());
  83. }
  84. }
  85. catch
  86. {
  87. return rs;
  88. }
  89. }
  90. }
  91. }