BlockCipher.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System;
  2. using Renci.SshNet.Security.Cryptography.Ciphers;
  3. namespace Renci.SshNet.Security.Cryptography
  4. {
  5. /// <summary>
  6. /// Base class for block cipher implementations.
  7. /// </summary>
  8. public abstract class BlockCipher : SymmetricCipher
  9. {
  10. private readonly CipherMode _mode;
  11. private readonly CipherPadding _padding;
  12. /// <summary>
  13. /// Gets the size of the block in bytes.
  14. /// </summary>
  15. /// <value>
  16. /// The size of the block in bytes.
  17. /// </value>
  18. private readonly byte _blockSize;
  19. /// <summary>
  20. /// Gets the minimum data size.
  21. /// </summary>
  22. /// <value>
  23. /// The minimum data size.
  24. /// </value>
  25. public override byte MinimumSize
  26. {
  27. get { return BlockSize; }
  28. }
  29. /// <summary>
  30. /// Gets the size of the block.
  31. /// </summary>
  32. /// <value>
  33. /// The size of the block.
  34. /// </value>
  35. public byte BlockSize
  36. {
  37. get
  38. {
  39. return _blockSize;
  40. }
  41. }
  42. /// <summary>
  43. /// Initializes a new instance of the <see cref="BlockCipher"/> class.
  44. /// </summary>
  45. /// <param name="key">The key.</param>
  46. /// <param name="blockSize">Size of the block.</param>
  47. /// <param name="mode">Cipher mode.</param>
  48. /// <param name="padding">Cipher padding.</param>
  49. /// <exception cref="ArgumentNullException"><paramref name="key"/> is <c>null</c>.</exception>
  50. protected BlockCipher(byte[] key, byte blockSize, CipherMode mode, CipherPadding padding)
  51. : base(key)
  52. {
  53. _blockSize = blockSize;
  54. _mode = mode;
  55. _padding = padding;
  56. if (_mode != null)
  57. _mode.Init(this);
  58. }
  59. /// <summary>
  60. /// Encrypts the specified data.
  61. /// </summary>
  62. /// <param name="data">The data.</param>
  63. /// <param name="offset">The zero-based offset in <paramref name="data"/> at which to begin encrypting.</param>
  64. /// <param name="length">The number of bytes to encrypt from <paramref name="data"/>.</param>
  65. /// <returns>Encrypted data</returns>
  66. public override byte[] Encrypt(byte[] data, int offset, int length)
  67. {
  68. if (length % _blockSize > 0)
  69. {
  70. if (_padding == null)
  71. {
  72. throw new ArgumentException("data");
  73. }
  74. var paddingLength = _blockSize - (length % _blockSize);
  75. data = _padding.Pad(data, offset, length, paddingLength);
  76. length += paddingLength;
  77. offset = 0;
  78. }
  79. var output = new byte[length];
  80. var writtenBytes = 0;
  81. for (var i = 0; i < length / _blockSize; i++)
  82. {
  83. if (_mode == null)
  84. {
  85. writtenBytes += EncryptBlock(data, offset + (i * _blockSize), _blockSize, output, i * _blockSize);
  86. }
  87. else
  88. {
  89. writtenBytes += _mode.EncryptBlock(data, offset + (i * _blockSize), _blockSize, output, i * _blockSize);
  90. }
  91. }
  92. if (writtenBytes < length)
  93. {
  94. throw new InvalidOperationException("Encryption error.");
  95. }
  96. return output;
  97. }
  98. /// <summary>
  99. /// Decrypts the specified data.
  100. /// </summary>
  101. /// <param name="data">The data.</param>
  102. /// <returns>Decrypted data</returns>
  103. public override byte[] Decrypt(byte[] data)
  104. {
  105. return Decrypt(data, 0, data.Length);
  106. }
  107. /// <summary>
  108. /// Decrypts the specified input.
  109. /// </summary>
  110. /// <param name="data">The input.</param>
  111. /// <param name="offset">The zero-based offset in <paramref name="data"/> at which to begin decrypting.</param>
  112. /// <param name="length">The number of bytes to decrypt from <paramref name="data"/>.</param>
  113. /// <returns>
  114. /// The decrypted data.
  115. /// </returns>
  116. public override byte[] Decrypt(byte[] data, int offset, int length)
  117. {
  118. if (length % _blockSize > 0)
  119. {
  120. if (_padding == null)
  121. {
  122. throw new ArgumentException("data");
  123. }
  124. data = _padding.Pad(_blockSize, data, offset, length);
  125. offset = 0;
  126. length = data.Length;
  127. }
  128. var output = new byte[length];
  129. var writtenBytes = 0;
  130. for (var i = 0; i < length / _blockSize; i++)
  131. {
  132. if (_mode == null)
  133. {
  134. writtenBytes += DecryptBlock(data, offset + (i * _blockSize), _blockSize, output, i * _blockSize);
  135. }
  136. else
  137. {
  138. writtenBytes += _mode.DecryptBlock(data, offset + (i * _blockSize), _blockSize, output, i * _blockSize);
  139. }
  140. }
  141. if (writtenBytes < length)
  142. {
  143. throw new InvalidOperationException("Encryption error.");
  144. }
  145. return output;
  146. }
  147. }
  148. }