/PassWord/DES.cs

https://github.com/bbisky/dotnet · C# · 106 lines · 61 code · 10 blank · 35 comment · 1 complexity · de4d3faeb9938214adff665229467ec8 MD5 · raw file

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