ConfigCrypter.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. using WingServerCommon.Log;
  6. namespace WingServerCommon.Config
  7. {
  8. public class ConfigCrypter
  9. {
  10. // Defalt key vector
  11. private byte[] _keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
  12. private string _defaultKey = "vinno123";
  13. /// <summary>
  14. /// DES encrypt string.
  15. /// </summary>
  16. /// <param name="encryptString">String to be encrypted</param>
  17. /// <param name="encryptKey">encrypt key,8 characters</param>
  18. /// <returns>Return encrypted string while success,empty string will be return while failed.</returns>
  19. public string EncryptDES(string encryptString, string encryptKey = "")
  20. {
  21. try
  22. {
  23. var needEncrypt = !IsBase64Formatted(encryptString);
  24. if (needEncrypt)
  25. {
  26. encryptKey = string.IsNullOrEmpty(encryptKey) ? _defaultKey : encryptKey;
  27. var rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
  28. var rgbIV = _keys;
  29. var inputByteArray = Encoding.UTF8.GetBytes(encryptString);
  30. var dCSP = new DESCryptoServiceProvider();
  31. var mStream = new MemoryStream();
  32. var cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
  33. cStream.Write(inputByteArray, 0, inputByteArray.Length);
  34. cStream.FlushFinalBlock();
  35. var asdsad = Convert.ToBase64String(mStream.ToArray());
  36. return Convert.ToBase64String(mStream.ToArray());
  37. }
  38. else
  39. {
  40. return encryptString;
  41. }
  42. }
  43. catch (Exception e)
  44. {
  45. Logger.WriteLineError($"Encrypt failed:{e}");
  46. return string.Empty;
  47. }
  48. }
  49. /// <summary>
  50. /// DES decrypt string
  51. /// </summary>
  52. /// <param name="decryptString">String to be decrypted</param>
  53. /// <param name="decryptKey">decrypt key,8 characters,same as encrypt key.</param>
  54. /// <returns>Return decrypted string while success,source string will be return while failed.</returns>
  55. public string DecryptDES(string decryptString, string decryptKey = "")
  56. {
  57. if (string.IsNullOrEmpty(decryptString))
  58. {
  59. return string.Empty;
  60. }
  61. var needDecrypt = IsBase64Formatted(decryptString);
  62. if (needDecrypt)
  63. {
  64. try
  65. {
  66. decryptKey = string.IsNullOrEmpty(decryptKey) ? _defaultKey : decryptKey;
  67. var rgbKey = Encoding.UTF8.GetBytes(decryptKey);
  68. var rgbIV = _keys;
  69. var inputByteArray = Convert.FromBase64String(decryptString);
  70. var DCSP = new DESCryptoServiceProvider();
  71. var mStream = new MemoryStream();
  72. var cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
  73. cStream.Write(inputByteArray, 0, inputByteArray.Length);
  74. cStream.FlushFinalBlock();
  75. var asdasd = Encoding.UTF8.GetString(mStream.ToArray());
  76. return Encoding.UTF8.GetString(mStream.ToArray());
  77. }
  78. catch (Exception e)
  79. {
  80. Logger.WriteLineError($"decrypt failed:{e}");
  81. return decryptString;
  82. }
  83. }
  84. return decryptString;
  85. }
  86. private bool IsBase64Formatted(string input)
  87. {
  88. try
  89. {
  90. Convert.FromBase64String(input);
  91. return true;
  92. }
  93. catch
  94. {
  95. return false;
  96. }
  97. }
  98. }
  99. }