You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

PlainEncryptor.cs 2.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Shadowsocks.Encryption.Stream
  4. {
  5. class PlainEncryptor
  6. : EncryptorBase, IDisposable
  7. {
  8. const int CIPHER_NONE = 1;
  9. private static Dictionary<string, EncryptorInfo> _ciphers = new Dictionary<string, EncryptorInfo> {
  10. { "plain", new EncryptorInfo("PLAIN", 0, 0, CIPHER_NONE) },
  11. { "none", new EncryptorInfo("PLAIN", 0, 0, CIPHER_NONE) }
  12. };
  13. public PlainEncryptor(string method, string password) : base(method, password)
  14. {
  15. }
  16. public static List<string> SupportedCiphers()
  17. {
  18. return new List<string>(_ciphers.Keys);
  19. }
  20. protected Dictionary<string, EncryptorInfo> getCiphers()
  21. {
  22. return _ciphers;
  23. }
  24. #region TCP
  25. public override void Encrypt(byte[] buf, int length, byte[] outbuf, out int outlength)
  26. {
  27. Buffer.BlockCopy(buf, 0, outbuf, 0, length);
  28. outlength = length;
  29. }
  30. public override void Decrypt(byte[] buf, int length, byte[] outbuf, out int outlength)
  31. {
  32. Buffer.BlockCopy(buf, 0, outbuf, 0, length);
  33. outlength = length;
  34. }
  35. #endregion
  36. #region UDP
  37. public override void EncryptUDP(byte[] buf, int length, byte[] outbuf, out int outlength)
  38. {
  39. Buffer.BlockCopy(buf, 0, outbuf, 0, length);
  40. outlength = length;
  41. }
  42. public override void DecryptUDP(byte[] buf, int length, byte[] outbuf, out int outlength)
  43. {
  44. Buffer.BlockCopy(buf, 0, outbuf, 0, length);
  45. outlength = length;
  46. }
  47. #endregion
  48. #region IDisposable
  49. private bool _disposed;
  50. // instance based lock
  51. private readonly object _lock = new object();
  52. public override void Dispose()
  53. {
  54. Dispose(true);
  55. GC.SuppressFinalize(this);
  56. }
  57. ~PlainEncryptor()
  58. {
  59. Dispose(false);
  60. }
  61. protected virtual void Dispose(bool disposing)
  62. {
  63. lock (_lock)
  64. {
  65. if (_disposed) return;
  66. _disposed = true;
  67. }
  68. if (disposing)
  69. {
  70. // free managed objects
  71. }
  72. }
  73. #endregion
  74. }
  75. }