DesBuilder.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. namespace AILicenseGenerator.Helper
  6. {
  7. internal class DesBuilder
  8. {
  9. private const string EncryptKey = "V1NN0KEY";
  10. private static readonly byte[] Keys = { 0x56, 0x34, 0x90, 0xCD, 0xEF, 0xAB, 0x12, 0x78 };
  11. /// <summary>
  12. /// 加密
  13. /// </summary>
  14. /// <param name="value"></param>
  15. /// <returns></returns>
  16. public static string Encrypt(string value)
  17. {
  18. if (string.IsNullOrEmpty(value)) return string.Empty;
  19. try
  20. {
  21. byte[] rgbKey = Encoding.UTF8.GetBytes(EncryptKey);
  22. byte[] rgbIv = Keys;
  23. byte[] inputByteArray = Encoding.UTF8.GetBytes(value);
  24. var dCsp = new DESCryptoServiceProvider();
  25. using (var stream = new MemoryStream())
  26. {
  27. using (var cStream = new CryptoStream(stream, dCsp.CreateEncryptor(rgbKey, rgbIv), CryptoStreamMode.Write))
  28. {
  29. cStream.Write(inputByteArray, 0, inputByteArray.Length);
  30. cStream.FlushFinalBlock();
  31. var output = Convert.ToBase64String(stream.ToArray());
  32. return "{" + output + "}";
  33. }
  34. }
  35. }
  36. catch (Exception ex)
  37. {
  38. Logger.Error($"Encrypt {value} error : {ex}");
  39. return value;
  40. }
  41. }
  42. /// <summary>
  43. /// 解密
  44. /// </summary>
  45. /// <param name="value"></param>
  46. /// <returns></returns>
  47. public static string Decrypt(string value)
  48. {
  49. var exceptionReturnResult = value;
  50. if (string.IsNullOrEmpty(value)) return string.Empty;
  51. try
  52. {
  53. var frontBrace = value.IndexOf('{');
  54. var backBrace = value.IndexOf('}');
  55. value = value.Substring(1, backBrace - frontBrace - 1);
  56. byte[] rgbKey = Encoding.UTF8.GetBytes(EncryptKey);
  57. byte[] rgbIv = Keys;
  58. byte[] inputByteArray = Convert.FromBase64String(value);
  59. var dcsp = new DESCryptoServiceProvider();
  60. using (var stream = new MemoryStream())
  61. {
  62. using (var cStream = new CryptoStream(stream, dcsp.CreateDecryptor(rgbKey, rgbIv), CryptoStreamMode.Write))
  63. {
  64. cStream.Write(inputByteArray, 0, inputByteArray.Length);
  65. cStream.FlushFinalBlock();
  66. return Encoding.UTF8.GetString(stream.ToArray());
  67. }
  68. }
  69. }
  70. catch (Exception ex)
  71. {
  72. Logger.Error($"Decrypt {exceptionReturnResult} error:{ex}");
  73. return exceptionReturnResult;
  74. }
  75. }
  76. /// <summary>
  77. /// 加密
  78. /// </summary>
  79. /// <param name="value"></param>
  80. /// <returns></returns>
  81. public static byte[] BytesEncrypt(byte[] value, string encryptKey = null)
  82. {
  83. if (value == null) return new byte[0];
  84. try
  85. {
  86. byte[] rgbKey = Encoding.UTF8.GetBytes(string.IsNullOrWhiteSpace(encryptKey) ? EncryptKey : encryptKey);
  87. byte[] rgbIv = Keys;
  88. byte[] inputByteArray = value;
  89. var dCsp = new DESCryptoServiceProvider();
  90. using (var stream = new MemoryStream())
  91. {
  92. using (var cStream = new CryptoStream(stream, dCsp.CreateEncryptor(rgbKey, rgbIv), CryptoStreamMode.Write))
  93. {
  94. cStream.Write(inputByteArray, 0, inputByteArray.Length);
  95. cStream.FlushFinalBlock();
  96. var output = stream.ToArray();
  97. return output;
  98. }
  99. }
  100. }
  101. catch (Exception ex)
  102. {
  103. Logger.Error($"BytesEncrypt {ex}");
  104. return value;
  105. }
  106. }
  107. /// <summary>
  108. /// 解密
  109. /// </summary>
  110. /// <param name="value"></param>
  111. /// <returns></returns>
  112. public static byte[] BytesDecrypt(byte[] value, string encryptKey = null)
  113. {
  114. if (value == null) return new byte[0];
  115. try
  116. {
  117. byte[] rgbKey = Encoding.UTF8.GetBytes(string.IsNullOrWhiteSpace(encryptKey) ? EncryptKey : encryptKey);
  118. byte[] rgbIv = Keys;
  119. byte[] inputByteArray = value;
  120. var dcsp = new DESCryptoServiceProvider();
  121. using (var stream = new MemoryStream())
  122. {
  123. using (var cStream = new CryptoStream(stream, dcsp.CreateDecryptor(rgbKey, rgbIv), CryptoStreamMode.Write))
  124. {
  125. cStream.Write(inputByteArray, 0, inputByteArray.Length);
  126. cStream.FlushFinalBlock();
  127. return stream.ToArray();
  128. }
  129. }
  130. }
  131. catch (Exception ex)
  132. {
  133. Logger.Error($"BytesDecrypt {ex}");
  134. return new byte[0];
  135. }
  136. }
  137. }
  138. }