EncryptionManager.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using MediaBrowser.Controller.Security;
  2. using System;
  3. using System.Text;
  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="System.ArgumentNullException">value</exception>
  14. public string EncryptString(string value)
  15. {
  16. if (value == null) throw new ArgumentNullException("value");
  17. return EncryptStringUniversal(value);
  18. }
  19. /// <summary>
  20. /// Decrypts the string.
  21. /// </summary>
  22. /// <param name="value">The value.</param>
  23. /// <returns>System.String.</returns>
  24. /// <exception cref="System.ArgumentNullException">value</exception>
  25. public string DecryptString(string value)
  26. {
  27. if (value == null) throw new ArgumentNullException("value");
  28. return DecryptStringUniversal(value);
  29. }
  30. private string EncryptStringUniversal(string value)
  31. {
  32. // Yes, this isn't good, but ProtectedData in mono is throwing exceptions, so use this for now
  33. var bytes = Encoding.UTF8.GetBytes(value);
  34. return Convert.ToBase64String(bytes);
  35. }
  36. private string DecryptStringUniversal(string value)
  37. {
  38. // Yes, this isn't good, but ProtectedData in mono is throwing exceptions, so use this for now
  39. var bytes = Convert.FromBase64String(value);
  40. return Encoding.UTF8.GetString(bytes, 0, bytes.Length);
  41. }
  42. }
  43. }