TripleDesCipher.BlockImpl.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #if !NET
  2. using System;
  3. using System.Security.Cryptography;
  4. using Org.BouncyCastle.Crypto.Paddings;
  5. namespace Renci.SshNet.Security.Cryptography.Ciphers
  6. {
  7. public partial class TripleDesCipher
  8. {
  9. private sealed class BlockImpl : BlockCipher, IDisposable
  10. {
  11. private readonly TripleDES _tripleDES;
  12. private readonly ICryptoTransform _encryptor;
  13. private readonly ICryptoTransform _decryptor;
  14. public BlockImpl(byte[] key, CipherMode mode, IBlockCipherPadding padding)
  15. : base(key, 8, mode, padding)
  16. {
  17. var tripleDES = TripleDES.Create();
  18. tripleDES.Key = key;
  19. tripleDES.Mode = System.Security.Cryptography.CipherMode.ECB;
  20. tripleDES.Padding = PaddingMode.None;
  21. _tripleDES = tripleDES;
  22. _encryptor = tripleDES.CreateEncryptor();
  23. _decryptor = tripleDES.CreateDecryptor();
  24. }
  25. public override int EncryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
  26. {
  27. return _encryptor.TransformBlock(inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset);
  28. }
  29. public override int DecryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
  30. {
  31. return _decryptor.TransformBlock(inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset);
  32. }
  33. public void Dispose()
  34. {
  35. _tripleDES.Dispose();
  36. _encryptor.Dispose();
  37. _decryptor.Dispose();
  38. }
  39. }
  40. }
  41. }
  42. #endif