DesEncryptHelper.cs 4.2 KB

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