CtrCipherMode.cs 4.2 KB

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