EncryptionHelper.cs 2.9 KB

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