BlockCipher.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. public abstract int BlockSize { get; }
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="BlockCipher"/> class.
  24. /// </summary>
  25. /// <param name="key">The key.</param>
  26. /// <param name="mode">Cipher mode.</param>
  27. /// <param name="padding">Cipher padding.</param>
  28. /// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
  29. protected BlockCipher(byte[] key, CipherMode mode, CipherPadding padding)
  30. : base(key)
  31. {
  32. this._mode = mode;
  33. this._padding = padding;
  34. this._mode.Init(this);
  35. }
  36. /// <summary>
  37. /// Encrypts the specified data.
  38. /// </summary>
  39. /// <param name="data">The data.</param>
  40. /// <returns>Encrypted data</returns>
  41. public override byte[] Encrypt(byte[] data)
  42. {
  43. var output = new byte[data.Length];
  44. if (data.Length % this.BlockSize > 0)
  45. {
  46. if (this._padding == null)
  47. {
  48. throw new ArgumentException("data");
  49. }
  50. else
  51. {
  52. data = this._padding.Pad(this.BlockSize, data);
  53. }
  54. }
  55. var writtenBytes = 0;
  56. for (int i = 0; i < data.Length / this.BlockSize; i++)
  57. {
  58. if (this._mode == null)
  59. {
  60. writtenBytes += this.EncryptBlock(data, i * this.BlockSize, this.BlockSize, output, i * this.BlockSize);
  61. }
  62. else
  63. {
  64. writtenBytes += this._mode.EncryptBlock(data, i * this.BlockSize, this.BlockSize, output, i * this.BlockSize);
  65. }
  66. }
  67. if (writtenBytes < data.Length)
  68. {
  69. throw new InvalidOperationException("Encryption error.");
  70. }
  71. return output;
  72. }
  73. /// <summary>
  74. /// Decrypts the specified data.
  75. /// </summary>
  76. /// <param name="data">The data.</param>
  77. /// <returns>Decrypted data</returns>
  78. public override byte[] Decrypt(byte[] data)
  79. {
  80. if (data.Length % this.BlockSize > 0)
  81. {
  82. {
  83. if (this._padding == null)
  84. {
  85. throw new ArgumentException("data");
  86. }
  87. else
  88. {
  89. data = this._padding.Pad(this.BlockSize, data);
  90. }
  91. }
  92. }
  93. var output = new byte[data.Length];
  94. var writtenBytes = 0;
  95. for (int i = 0; i < data.Length / this.BlockSize; i++)
  96. {
  97. if (this._mode == null)
  98. {
  99. writtenBytes += this.DecryptBlock(data, i * this.BlockSize, this.BlockSize, output, i * this.BlockSize);
  100. }
  101. else
  102. {
  103. writtenBytes += this._mode.DecryptBlock(data, i * this.BlockSize, this.BlockSize, output, i * this.BlockSize);
  104. }
  105. }
  106. if (writtenBytes < data.Length)
  107. {
  108. throw new InvalidOperationException("Encryption error.");
  109. }
  110. return output;
  111. }
  112. }
  113. }