EncryptionHelper.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. string password = "N1PEUPQX";
  12. byte[] bytes = Encoding.Unicode.GetBytes(clearText);
  13. using (Aes aes = Aes.Create())
  14. {
  15. Rfc2898DeriveBytes rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, new byte[13]
  16. {
  17. (byte) 73,
  18. (byte) 118,
  19. (byte) 97,
  20. (byte) 110,
  21. (byte) 32,
  22. (byte) 77,
  23. (byte) 101,
  24. (byte) 100,
  25. (byte) 118,
  26. (byte) 101,
  27. (byte) 100,
  28. (byte) 101,
  29. (byte) 118
  30. });
  31. aes.Key = rfc2898DeriveBytes.GetBytes(32);
  32. aes.IV = rfc2898DeriveBytes.GetBytes(16);
  33. using (MemoryStream memoryStream = new MemoryStream())
  34. {
  35. using (CryptoStream cryptoStream = new CryptoStream((Stream) memoryStream, aes.CreateEncryptor(),
  36. 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. string password = "N1PEUPQX";
  49. cipherText = cipherText.Replace(" ", "+");
  50. byte[] buffer = Convert.FromBase64String(cipherText);
  51. using (Aes aes = Aes.Create())
  52. {
  53. Rfc2898DeriveBytes rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, new byte[13]
  54. {
  55. (byte) 73,
  56. (byte) 118,
  57. (byte) 97,
  58. (byte) 110,
  59. (byte) 32,
  60. (byte) 77,
  61. (byte) 101,
  62. (byte) 100,
  63. (byte) 118,
  64. (byte) 101,
  65. (byte) 100,
  66. (byte) 101,
  67. (byte) 118
  68. });
  69. aes.Key = rfc2898DeriveBytes.GetBytes(32);
  70. aes.IV = rfc2898DeriveBytes.GetBytes(16);
  71. using (MemoryStream memoryStream = new MemoryStream())
  72. {
  73. using (CryptoStream cryptoStream = new CryptoStream((Stream) memoryStream, aes.CreateDecryptor(),
  74. CryptoStreamMode.Write))
  75. {
  76. cryptoStream.Write(buffer, 0, buffer.Length);
  77. cryptoStream.Close();
  78. }
  79. cipherText = Encoding.Unicode.GetString(memoryStream.ToArray());
  80. }
  81. }
  82. return cipherText;
  83. }
  84. }
  85. }