BlockCipher.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 null.</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. /// <returns>Encrypted data</returns>
  64. public override byte[] Encrypt(byte[] data)
  65. {
  66. if (data.Length % _blockSize > 0)
  67. {
  68. if (_padding == null)
  69. {
  70. throw new ArgumentException("data");
  71. }
  72. data = _padding.Pad(_blockSize, data);
  73. }
  74. var output = new byte[data.Length];
  75. var writtenBytes = 0;
  76. for (var i = 0; i < data.Length / _blockSize; i++)
  77. {
  78. if (_mode == null)
  79. {
  80. writtenBytes += EncryptBlock(data, i * _blockSize, _blockSize, output, i * _blockSize);
  81. }
  82. else
  83. {
  84. writtenBytes += _mode.EncryptBlock(data, i * _blockSize, _blockSize, output, i * _blockSize);
  85. }
  86. }
  87. if (writtenBytes < data.Length)
  88. {
  89. throw new InvalidOperationException("Encryption error.");
  90. }
  91. return output;
  92. }
  93. /// <summary>
  94. /// Decrypts the specified data.
  95. /// </summary>
  96. /// <param name="data">The data.</param>
  97. /// <returns>Decrypted data</returns>
  98. public override byte[] Decrypt(byte[] data)
  99. {
  100. if (data.Length % _blockSize > 0)
  101. {
  102. if (_padding == null)
  103. {
  104. throw new ArgumentException("data");
  105. }
  106. data = _padding.Pad(_blockSize, data);
  107. }
  108. var output = new byte[data.Length];
  109. var writtenBytes = 0;
  110. for (var i = 0; i < data.Length / _blockSize; i++)
  111. {
  112. if (_mode == null)
  113. {
  114. writtenBytes += DecryptBlock(data, i * _blockSize, _blockSize, output, i * _blockSize);
  115. }
  116. else
  117. {
  118. writtenBytes += _mode.DecryptBlock(data, i * _blockSize, _blockSize, output, i * _blockSize);
  119. }
  120. }
  121. if (writtenBytes < data.Length)
  122. {
  123. throw new InvalidOperationException("Encryption error.");
  124. }
  125. return output;
  126. }
  127. }
  128. }