CipherMode.cs 3.1 KB

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