OfbCipherMode.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using System.Globalization;
  3. namespace Renci.SshNet.Security.Cryptography.Ciphers.Modes
  4. {
  5. /// <summary>
  6. /// Implements OFB cipher mode.
  7. /// </summary>
  8. public class OfbCipherMode : CipherMode
  9. {
  10. private readonly byte[] _ivOutput;
  11. /// <summary>
  12. /// Initializes a new instance of the <see cref="OfbCipherMode"/> class.
  13. /// </summary>
  14. /// <param name="iv">The iv.</param>
  15. public OfbCipherMode(byte[] iv)
  16. : base(iv)
  17. {
  18. _ivOutput = new byte[iv.Length];
  19. }
  20. /// <summary>
  21. /// Encrypts the specified region of the input byte array and copies the encrypted data to the specified region of the output byte array.
  22. /// </summary>
  23. /// <param name="inputBuffer">The input data to encrypt.</param>
  24. /// <param name="inputOffset">The offset into the input byte array from which to begin using data.</param>
  25. /// <param name="inputCount">The number of bytes in the input byte array to use as data.</param>
  26. /// <param name="outputBuffer">The output to which to write encrypted data.</param>
  27. /// <param name="outputOffset">The offset into the output byte array from which to begin writing data.</param>
  28. /// <returns>
  29. /// The number of bytes encrypted.
  30. /// </returns>
  31. public override int EncryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
  32. {
  33. if (inputBuffer.Length - inputOffset < _blockSize)
  34. {
  35. throw new ArgumentException("Invalid input buffer");
  36. }
  37. if (outputBuffer.Length - outputOffset < _blockSize)
  38. {
  39. throw new ArgumentException("Invalid output buffer");
  40. }
  41. if (inputCount != _blockSize)
  42. {
  43. throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "inputCount must be {0}.", _blockSize));
  44. }
  45. _ = Cipher.EncryptBlock(IV, 0, IV.Length, _ivOutput, 0);
  46. Buffer.BlockCopy(_ivOutput, 0, IV, 0, IV.Length);
  47. for (var i = 0; i < _blockSize; i++)
  48. {
  49. outputBuffer[outputOffset + i] = (byte)(_ivOutput[i] ^ inputBuffer[inputOffset + i]);
  50. }
  51. return _blockSize;
  52. }
  53. /// <summary>
  54. /// Decrypts the specified region of the input byte array and copies the decrypted data to the specified region of the output byte array.
  55. /// </summary>
  56. /// <param name="inputBuffer">The input data to decrypt.</param>
  57. /// <param name="inputOffset">The offset into the input byte array from which to begin using data.</param>
  58. /// <param name="inputCount">The number of bytes in the input byte array to use as data.</param>
  59. /// <param name="outputBuffer">The output to which to write decrypted data.</param>
  60. /// <param name="outputOffset">The offset into the output byte array from which to begin writing data.</param>
  61. /// <returns>
  62. /// The number of bytes decrypted.
  63. /// </returns>
  64. public override int DecryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
  65. {
  66. return EncryptBlock(inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset);
  67. }
  68. }
  69. }