CbcMode.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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
  7. {
  8. /// <summary>
  9. /// Represents the class for the CBC Block Cipher.
  10. /// </summary>
  11. public class CbcMode : ModeBase
  12. {
  13. private byte[] _iv;
  14. private byte[] _nextIV;
  15. private int _blockSize;
  16. /// <summary>
  17. /// Gets the size of the block.
  18. /// </summary>
  19. /// <value>
  20. /// The size of the block.
  21. /// </value>
  22. public override int BlockSize
  23. {
  24. get { return this._blockSize; }
  25. }
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="CbcMode"/> class.
  28. /// </summary>
  29. /// <param name="cipher">The cipher.</param>
  30. public CbcMode(CipherBase cipher)
  31. : base(cipher)
  32. {
  33. this._blockSize = cipher.BlockSize;
  34. this._iv = cipher.IV.Take(this._blockSize).ToArray();
  35. this._nextIV = new byte[this._iv.Length];
  36. }
  37. /// <summary>
  38. /// Encrypts the specified region of the input byte array and copies the encrypted data to the specified region of the output byte array.
  39. /// </summary>
  40. /// <param name="inputBuffer">The input data to encrypt.</param>
  41. /// <param name="inputOffset">The offset into the input byte array from which to begin using data.</param>
  42. /// <param name="inputCount">The number of bytes in the input byte array to use as data.</param>
  43. /// <param name="outputBuffer">The output to which to write encrypted data.</param>
  44. /// <param name="outputOffset">The offset into the output byte array from which to begin writing data.</param>
  45. /// <returns>
  46. /// The number of bytes encrypted.
  47. /// </returns>
  48. public override int EncryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
  49. {
  50. if (inputBuffer.Length - inputOffset < this._blockSize)
  51. throw new ArgumentException("Invalid input buffer");
  52. if (outputBuffer.Length - outputOffset < this._blockSize)
  53. throw new ArgumentException("Invalid output buffer");
  54. if (inputCount != this._blockSize)
  55. throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "inputCount must be {0}.", this._blockSize));
  56. for (int i = 0; i < this._blockSize; i++)
  57. {
  58. this._iv[i] ^= inputBuffer[inputOffset + i];
  59. }
  60. this.Cipher.EncryptBlock(this._iv, 0, inputCount, outputBuffer, outputOffset);
  61. Array.Copy(outputBuffer, outputOffset, this._iv, 0, this._iv.Length);
  62. return this._blockSize;
  63. }
  64. /// <summary>
  65. /// Decrypts the specified region of the input byte array and copies the decrypted data to the specified region of the output byte array.
  66. /// </summary>
  67. /// <param name="inputBuffer">The input data to decrypt.</param>
  68. /// <param name="inputOffset">The offset into the input byte array from which to begin using data.</param>
  69. /// <param name="inputCount">The number of bytes in the input byte array to use as data.</param>
  70. /// <param name="outputBuffer">The output to which to write decrypted data.</param>
  71. /// <param name="outputOffset">The offset into the output byte array from which to begin writing data.</param>
  72. /// <returns>
  73. /// The number of bytes decrypted.
  74. /// </returns>
  75. public override int DecryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
  76. {
  77. if (inputBuffer.Length - inputOffset < this._blockSize)
  78. throw new ArgumentException("Invalid input buffer");
  79. if (outputBuffer.Length - outputOffset < this._blockSize)
  80. throw new ArgumentException("Invalid output buffer");
  81. if (inputCount != this._blockSize)
  82. throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "inputCount must be {0}.", this._blockSize));
  83. Array.Copy(inputBuffer, inputOffset, this._nextIV, 0, this._nextIV.Length);
  84. this.Cipher.DecryptBlock(inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset);
  85. for (int i = 0; i < this._blockSize; i++)
  86. {
  87. outputBuffer[outputOffset + i] ^= this._iv[i];
  88. }
  89. Array.Copy(this._nextIV, 0, this._iv, 0, this._nextIV.Length);
  90. return this._blockSize;
  91. }
  92. }
  93. }