OfbCipherMode.cs 4.6 KB

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