EncryptionManager.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.Text;
  3. using MediaBrowser.Controller.Security;
  4. namespace Emby.Server.Implementations.Security
  5. {
  6. public class EncryptionManager : IEncryptionManager
  7. {
  8. /// <summary>
  9. /// Encrypts the string.
  10. /// </summary>
  11. /// <param name="value">The value.</param>
  12. /// <returns>System.String.</returns>
  13. /// <exception cref="ArgumentNullException">value</exception>
  14. public string EncryptString(string value)
  15. {
  16. if (value == null)
  17. {
  18. throw new ArgumentNullException(nameof(value));
  19. }
  20. return EncryptStringUniversal(value);
  21. }
  22. /// <summary>
  23. /// Decrypts the string.
  24. /// </summary>
  25. /// <param name="value">The value.</param>
  26. /// <returns>System.String.</returns>
  27. /// <exception cref="ArgumentNullException">value</exception>
  28. public string DecryptString(string value)
  29. {
  30. if (value == null)
  31. {
  32. throw new ArgumentNullException(nameof(value));
  33. }
  34. return DecryptStringUniversal(value);
  35. }
  36. private static string EncryptStringUniversal(string value)
  37. {
  38. // Yes, this isn't good, but ProtectedData in mono is throwing exceptions, so use this for now
  39. var bytes = Encoding.UTF8.GetBytes(value);
  40. return Convert.ToBase64String(bytes);
  41. }
  42. private static string DecryptStringUniversal(string value)
  43. {
  44. // Yes, this isn't good, but ProtectedData in mono is throwing exceptions, so use this for now
  45. var bytes = Convert.FromBase64String(value);
  46. return Encoding.UTF8.GetString(bytes, 0, bytes.Length);
  47. }
  48. }
  49. }