EncryptionHelper.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. namespace LauncherIcarus.Daemon
  6. {
  7. public static class EncryptionHelper
  8. {
  9. private const string Password = "N1PEUPQX";
  10. private static Rfc2898DeriveBytes Rfc2898DeriveBytes { get; } = new Rfc2898DeriveBytes(Password, new byte[]
  11. {
  12. 73,
  13. 118,
  14. 97,
  15. 110,
  16. 32,
  17. 77,
  18. 101,
  19. 100,
  20. 118,
  21. 101,
  22. 100,
  23. 101,
  24. 118
  25. });
  26. public static string Encrypt(string clearText)
  27. {
  28. var bytes = Encoding.Unicode.GetBytes(clearText);
  29. using (var aes = Aes.Create())
  30. {
  31. aes.Key = Rfc2898DeriveBytes.GetBytes(32);
  32. aes.IV = Rfc2898DeriveBytes.GetBytes(16);
  33. using (var memoryStream = new MemoryStream())
  34. {
  35. using (var cryptoStream =
  36. new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
  37. {
  38. cryptoStream.Write(bytes, 0, bytes.Length);
  39. cryptoStream.Close();
  40. }
  41. clearText = Convert.ToBase64String(memoryStream.ToArray());
  42. }
  43. }
  44. return clearText;
  45. }
  46. public static string Decrypt(string cipherText)
  47. {
  48. cipherText = cipherText.Replace(" ", "+");
  49. var buffer = Convert.FromBase64String(cipherText);
  50. using (var aes = Aes.Create())
  51. {
  52. aes.Key = Rfc2898DeriveBytes.GetBytes(32);
  53. aes.IV = Rfc2898DeriveBytes.GetBytes(16);
  54. using (var memoryStream = new MemoryStream())
  55. {
  56. using (var cryptoStream =
  57. new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Write))
  58. {
  59. cryptoStream.Write(buffer, 0, buffer.Length);
  60. cryptoStream.Close();
  61. }
  62. cipherText = Encoding.Unicode.GetString(memoryStream.ToArray());
  63. }
  64. }
  65. return cipherText;
  66. }
  67. }
  68. }