CipherTransform.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Security.Cryptography;
  6. using System.Globalization;
  7. namespace Renci.SshNet.Security.Cryptography
  8. {
  9. /// <summary>
  10. ///
  11. /// </summary>
  12. internal class CipherTransform : ICryptoTransform
  13. {
  14. private readonly int _blockSize;
  15. private readonly CipherBase _blockCipher;
  16. /// <summary>
  17. /// Gets the transform mode.
  18. /// </summary>
  19. public TransformMode TransformMode { get; private set; }
  20. /// <summary>
  21. /// Initializes a new instance of the <see cref="CipherTransform"/> class.
  22. /// </summary>
  23. /// <param name="transformMode">The transform mode.</param>
  24. /// <param name="blockCipher">The block cipher.</param>
  25. public CipherTransform(TransformMode transformMode, CipherBase blockCipher)
  26. {
  27. this._blockCipher = blockCipher;
  28. this._blockSize = blockCipher.BlockSize;
  29. this.TransformMode = transformMode;
  30. }
  31. #region ICryptoTransform Members
  32. /// <summary>
  33. /// Gets a value indicating whether the current transform can be reused.
  34. /// </summary>
  35. /// <returns>true if the current transform can be reused; otherwise, false.</returns>
  36. public bool CanReuseTransform
  37. {
  38. get { return true; }
  39. }
  40. /// <summary>
  41. /// Gets a value indicating whether multiple blocks can be transformed.
  42. /// </summary>
  43. /// <returns>true if multiple blocks can be transformed; otherwise, false.</returns>
  44. public bool CanTransformMultipleBlocks
  45. {
  46. get { return false; }
  47. }
  48. /// <summary>
  49. /// Gets the input block size.
  50. /// </summary>
  51. /// <returns>The size of the input data blocks in bytes.</returns>
  52. public int InputBlockSize
  53. {
  54. get { return this._blockSize; }
  55. }
  56. /// <summary>
  57. /// Gets the output block size.
  58. /// </summary>
  59. /// <returns>The size of the output data blocks in bytes.</returns>
  60. public int OutputBlockSize
  61. {
  62. get { return this._blockSize; }
  63. }
  64. /// <summary>
  65. /// Transforms the specified region of the input byte array and copies the resulting transform to the specified region of the output byte array.
  66. /// </summary>
  67. /// <param name="inputBuffer">The input for which to compute the transform.</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 the transform.</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 written.
  74. /// </returns>
  75. public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
  76. {
  77. if (inputCount % this._blockSize != 0)
  78. throw new ArgumentException("Invalid value.");
  79. var length = 0;
  80. for (int i = 0; i < inputCount / this._blockSize; i++)
  81. {
  82. switch (this.TransformMode)
  83. {
  84. case TransformMode.Encrypt:
  85. length += this._blockCipher.EncryptBlock(inputBuffer, inputOffset + this._blockSize * i, this._blockSize, outputBuffer, outputOffset + this._blockSize * i);
  86. break;
  87. case TransformMode.Decrypt:
  88. length += this._blockCipher.DecryptBlock(inputBuffer, inputOffset + this._blockSize * i, this._blockSize, outputBuffer, outputOffset + this._blockSize * i);
  89. break;
  90. default:
  91. throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "TransformMode '{0}' is not supported.", this.TransformMode));
  92. }
  93. }
  94. return length;
  95. }
  96. public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
  97. {
  98. throw new NotSupportedException("TransformFinalBlock is currently not supported.");
  99. }
  100. #endregion
  101. #region IDisposable Members
  102. public void Dispose()
  103. {
  104. throw new NotImplementedException();
  105. }
  106. #endregion
  107. }
  108. }