CipherAESCTR.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Cryptography;
  5. using Renci.SshNet.Security.Cryptography;
  6. namespace Renci.SshNet.Security
  7. {
  8. /// <summary>
  9. /// Represents base class for AES based encryption.
  10. /// </summary>
  11. public abstract class CipherAesCtr : Cipher
  12. {
  13. private readonly int _keySize;
  14. /// <summary>
  15. /// Gets or sets the key size, in bits, of the secret key used by the cipher.
  16. /// </summary>
  17. /// <value>
  18. /// The key size, in bits.
  19. /// </value>
  20. public override int KeySize
  21. {
  22. get
  23. {
  24. return this._keySize / 8;
  25. }
  26. }
  27. /// <summary>
  28. /// Gets or sets the block size, in bits, of the cipher operation.
  29. /// </summary>
  30. /// <value>
  31. /// The block size, in bits.
  32. /// </value>
  33. public override int BlockSize
  34. {
  35. get
  36. {
  37. return 16;
  38. }
  39. }
  40. /// <summary>
  41. /// Initializes a new instance of the <see cref="CipherAesCtr"/> class.
  42. /// </summary>
  43. /// <param name="keyBitsSize">Size of the key bits.</param>
  44. public CipherAesCtr(int keyBitsSize)
  45. {
  46. this._keySize = keyBitsSize;
  47. }
  48. /// <summary>
  49. /// Creates the encryptor.
  50. /// </summary>
  51. /// <returns></returns>
  52. protected override ModeBase CreateEncryptor()
  53. {
  54. return new CtrMode(new AesCipher(this.Key.Take(this._keySize / 8).ToArray(), this.Vector.Take(this.BlockSize).ToArray()));
  55. }
  56. /// <summary>
  57. /// Creates the decryptor.
  58. /// </summary>
  59. /// <returns></returns>
  60. protected override ModeBase CreateDecryptor()
  61. {
  62. return new CtrMode(new AesCipher(this.Key.Take(this._keySize / 8).ToArray(), this.Vector.Take(this.BlockSize).ToArray()));
  63. }
  64. }
  65. /// <summary>
  66. /// Represents AES 128 bit encryption.
  67. /// </summary>
  68. public class CipherAes128Ctr : CipherAesCtr
  69. {
  70. /// <summary>
  71. /// Gets algorithm name.
  72. /// </summary>
  73. public override string Name
  74. {
  75. get { return "aes128-ctr"; }
  76. }
  77. /// <summary>
  78. /// Initializes a new instance of the <see cref="CipherAes192Cbc"/> class.
  79. /// </summary>
  80. public CipherAes128Ctr()
  81. : base(128)
  82. {
  83. }
  84. }
  85. /// <summary>
  86. /// Represents AES 192 bit encryption.
  87. /// </summary>
  88. public class CipherAes192Ctr : CipherAesCtr
  89. {
  90. /// <summary>
  91. /// Gets algorithm name.
  92. /// </summary>
  93. public override string Name
  94. {
  95. get { return "aes192-ctr"; }
  96. }
  97. /// <summary>
  98. /// Initializes a new instance of the <see cref="CipherAes192Cbc"/> class.
  99. /// </summary>
  100. public CipherAes192Ctr()
  101. : base(192)
  102. {
  103. }
  104. }
  105. /// <summary>
  106. /// Represents AES 256 bit encryption.
  107. /// </summary>
  108. public class CipherAes256Ctr : CipherAesCtr
  109. {
  110. /// <summary>
  111. /// Gets algorithm name.
  112. /// </summary>
  113. public override string Name
  114. {
  115. get { return "aes256-ctr"; }
  116. }
  117. /// <summary>
  118. /// Initializes a new instance of the <see cref="CipherAes192Cbc"/> class.
  119. /// </summary>
  120. public CipherAes256Ctr()
  121. : base(256)
  122. {
  123. }
  124. }
  125. }