BlockCipher.cs 4.5 KB

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