2
0

CfbCipherMode.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Globalization;
  3. namespace Renci.SshNet.Security.Cryptography.Ciphers.Modes
  4. {
  5. /// <summary>
  6. /// Implements CFB cipher mode
  7. /// </summary>
  8. public class CfbCipherMode : CipherMode
  9. {
  10. private readonly byte[] _ivOutput;
  11. /// <summary>
  12. /// Initializes a new instance of the <see cref="CfbCipherMode"/> class.
  13. /// </summary>
  14. /// <param name="iv">The iv.</param>
  15. public CfbCipherMode(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. throw new ArgumentException("Invalid input buffer");
  35. if (outputBuffer.Length - outputOffset < _blockSize)
  36. throw new ArgumentException("Invalid output buffer");
  37. if (inputCount != _blockSize)
  38. throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "inputCount must be {0}.", _blockSize));
  39. Cipher.EncryptBlock(IV, 0, IV.Length, _ivOutput, 0);
  40. for (int i = 0; i < _blockSize; i++)
  41. {
  42. outputBuffer[outputOffset + i] = (byte)(_ivOutput[i] ^ inputBuffer[inputOffset + i]);
  43. }
  44. Buffer.BlockCopy(IV, _blockSize, IV, 0, IV.Length - _blockSize);
  45. Buffer.BlockCopy(outputBuffer, outputOffset, IV, IV.Length - _blockSize, _blockSize);
  46. return _blockSize;
  47. }
  48. /// <summary>
  49. /// Decrypts the specified region of the input byte array and copies the decrypted data to the specified region of the output byte array.
  50. /// </summary>
  51. /// <param name="inputBuffer">The input data to decrypt.</param>
  52. /// <param name="inputOffset">The offset into the input byte array from which to begin using data.</param>
  53. /// <param name="inputCount">The number of bytes in the input byte array to use as data.</param>
  54. /// <param name="outputBuffer">The output to which to write decrypted data.</param>
  55. /// <param name="outputOffset">The offset into the output byte array from which to begin writing data.</param>
  56. /// <returns>
  57. /// The number of bytes decrypted.
  58. /// </returns>
  59. public override int DecryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
  60. {
  61. if (inputBuffer.Length - inputOffset < _blockSize)
  62. throw new ArgumentException("Invalid input buffer");
  63. if (outputBuffer.Length - outputOffset < _blockSize)
  64. throw new ArgumentException("Invalid output buffer");
  65. if (inputCount != _blockSize)
  66. throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "inputCount must be {0}.", _blockSize));
  67. Cipher.EncryptBlock(IV, 0, IV.Length, _ivOutput, 0);
  68. Buffer.BlockCopy(IV, _blockSize, IV, 0, IV.Length - _blockSize);
  69. Buffer.BlockCopy(inputBuffer, inputOffset, IV, IV.Length - _blockSize, _blockSize);
  70. for (int i = 0; i < _blockSize; i++)
  71. {
  72. outputBuffer[outputOffset + i] = (byte)(_ivOutput[i] ^ inputBuffer[inputOffset + i]);
  73. }
  74. return _blockSize;
  75. }
  76. }
  77. }