DesEncrypt.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. namespace WS
  6. {
  7. ///<summary>Des加密</summary>
  8. public class DesEncrypt
  9. {
  10. private const string Key = "Raof!@83";
  11. /// <summary>
  12. /// DES+Base64加密
  13. /// <para>采用ECB、PKCS7</para>
  14. /// </summary>
  15. /// <param name="encryptString">加密字符串</param>
  16. /// <param name="key">秘钥</param>
  17. public static string Encrypt(string encryptString, string key = null)
  18. {
  19. return Encrypt(encryptString, key, false, true);
  20. }
  21. /// <summary>
  22. /// DES+Base64解密
  23. /// <para>采用ECB、PKCS7</para>
  24. /// </summary>
  25. /// <param name="decryptString">解密字符串</param>
  26. /// <param name="key">秘钥</param>
  27. public static string Decrypt(string decryptString, string key = null)
  28. {
  29. return Decrypt(decryptString, key, false);
  30. }
  31. /// <summary>
  32. /// DES+16进制加密
  33. /// <para>采用ECB、PKCS7</para>
  34. /// </summary>
  35. /// <param name="encryptString">加密字符串</param>
  36. /// <param name="key">秘钥</param>
  37. /// <param name="lowerCase">是否小写</param>
  38. public static string Encrypt4Hex(string encryptString, string key = null, bool lowerCase = false)
  39. {
  40. return Encrypt(encryptString, key, true, lowerCase);
  41. }
  42. /// <summary>
  43. /// DES+16进制解密
  44. /// <para>采用ECB、PKCS7</para>
  45. /// </summary>
  46. /// <param name="decryptString">解密字符串</param>
  47. /// <param name="key">秘钥</param>
  48. public static string Decrypt4Hex(string decryptString, string key = null)
  49. {
  50. return Decrypt(decryptString, key, true);
  51. }
  52. /// <summary>DES加密</summary>
  53. private static string Encrypt(string encryptString, string key, bool hex, bool lowerCase = false)
  54. {
  55. if (string.IsNullOrEmpty(encryptString))
  56. return null;
  57. if (string.IsNullOrEmpty(key))
  58. key = Key;
  59. if (key.Length < 8)
  60. throw new ArgumentException("秘钥长度为8位", nameof(key));
  61. var keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));
  62. var inputByteArray = Encoding.UTF8.GetBytes(encryptString);
  63. var provider = new DESCryptoServiceProvider
  64. {
  65. Mode = CipherMode.ECB,
  66. Key = keyBytes,
  67. Padding = PaddingMode.PKCS7
  68. };
  69. using (var stream = new MemoryStream())
  70. {
  71. var cStream = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
  72. cStream.Write(inputByteArray, 0, inputByteArray.Length);
  73. cStream.FlushFinalBlock();
  74. var bytes = stream.ToArray();
  75. return hex ? bytes.ToHex(lowerCase) : bytes.ToBase64();
  76. }
  77. }
  78. /// <summary>DES解密</summary>
  79. private static string Decrypt(string decryptString, string key, bool hex)
  80. {
  81. if (string.IsNullOrEmpty(decryptString))
  82. return null;
  83. if (string.IsNullOrEmpty(key))
  84. key = Key;
  85. if (key.Length < 8)
  86. throw new ArgumentException("秘钥长度为8位", nameof(key));
  87. var keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));
  88. var inputByteArray = hex ? decryptString.HexToBytes() : Convert.FromBase64String(decryptString);
  89. var provider = new DESCryptoServiceProvider
  90. {
  91. Mode = CipherMode.ECB,
  92. Key = keyBytes,
  93. Padding = PaddingMode.PKCS7
  94. };
  95. using (var mStream = new MemoryStream())
  96. {
  97. var cStream = new CryptoStream(mStream, provider.CreateDecryptor(), CryptoStreamMode.Write);
  98. cStream.Write(inputByteArray, 0, inputByteArray.Length);
  99. cStream.FlushFinalBlock();
  100. return Encoding.UTF8.GetString(mStream.ToArray());
  101. }
  102. }
  103. }
  104. }