BlockCipher.cs 4.5 KB

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