CipherMode.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System.Linq;
  2. namespace Renci.SshNet.Security.Cryptography.Ciphers
  3. {
  4. /// <summary>
  5. /// Base class for cipher mode implementations
  6. /// </summary>
  7. public abstract class CipherMode
  8. {
  9. /// <summary>
  10. /// Gets the cipher.
  11. /// </summary>
  12. protected BlockCipher Cipher;
  13. /// <summary>
  14. /// Gets the IV vector.
  15. /// </summary>
  16. protected byte[] IV;
  17. /// <summary>
  18. /// Holds block size of the cipher.
  19. /// </summary>
  20. protected int _blockSize;
  21. /// <summary>
  22. /// Initializes a new instance of the <see cref="CipherMode"/> class.
  23. /// </summary>
  24. /// <param name="iv">The iv.</param>
  25. protected CipherMode(byte[] iv)
  26. {
  27. IV = iv;
  28. }
  29. /// <summary>
  30. /// Initializes the specified cipher mode.
  31. /// </summary>
  32. /// <param name="cipher">The cipher.</param>
  33. internal void Init(BlockCipher cipher)
  34. {
  35. Cipher = cipher;
  36. _blockSize = cipher.BlockSize;
  37. IV = IV.Take(_blockSize).ToArray();
  38. }
  39. /// <summary>
  40. /// Encrypts the specified region of the input byte array and copies the encrypted data to the specified region of the output byte array.
  41. /// </summary>
  42. /// <param name="inputBuffer">The input data to encrypt.</param>
  43. /// <param name="inputOffset">The offset into the input byte array from which to begin using data.</param>
  44. /// <param name="inputCount">The number of bytes in the input byte array to use as data.</param>
  45. /// <param name="outputBuffer">The output to which to write encrypted data.</param>
  46. /// <param name="outputOffset">The offset into the output byte array from which to begin writing data.</param>
  47. /// <returns>
  48. /// The number of bytes encrypted.
  49. /// </returns>
  50. public abstract int EncryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset);
  51. /// <summary>
  52. /// Decrypts the specified region of the input byte array and copies the decrypted data to the specified region of the output byte array.
  53. /// </summary>
  54. /// <param name="inputBuffer">The input data to decrypt.</param>
  55. /// <param name="inputOffset">The offset into the input byte array from which to begin using data.</param>
  56. /// <param name="inputCount">The number of bytes in the input byte array to use as data.</param>
  57. /// <param name="outputBuffer">The output to which to write decrypted data.</param>
  58. /// <param name="outputOffset">The offset into the output byte array from which to begin writing data.</param>
  59. /// <returns>
  60. /// The number of bytes decrypted.
  61. /// </returns>
  62. public abstract int DecryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset);
  63. }
  64. }