CipherTripleDesCbc.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 Triple DES encryption.
  10. /// </summary>
  11. public abstract class CipherTripleDesCbc : 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;
  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 8;
  38. }
  39. }
  40. /// <summary>
  41. /// Initializes a new instance of the <see cref="CipherTripleDes192Cbc"/> class.
  42. /// </summary>
  43. /// <param name="keySize">Size of the key.</param>
  44. public CipherTripleDesCbc(int keySize)
  45. {
  46. this._keySize = keySize;
  47. }
  48. /// <summary>
  49. /// Creates the encryptor.
  50. /// </summary>
  51. /// <returns></returns>
  52. protected override ModeBase CreateEncryptor()
  53. {
  54. return new CbcMode(new TripleDesCipher(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 CbcMode(new TripleDesCipher(this.Key.Take(this.KeySize / 8).ToArray(), this.Vector.Take(this.BlockSize).ToArray()));
  63. }
  64. }
  65. /// <summary>
  66. /// Represents class Triple DES 192 CBC encryption.
  67. /// </summary>
  68. public class CipherTripleDes192Cbc : CipherTripleDesCbc
  69. {
  70. /// <summary>
  71. /// Gets algorithm name.
  72. /// </summary>
  73. public override string Name
  74. {
  75. get { return "3des-cbc"; }
  76. }
  77. /// <summary>
  78. /// Initializes a new instance of the <see cref="CipherTripleDes192Cbc"/> class.
  79. /// </summary>
  80. public CipherTripleDes192Cbc()
  81. : base(192)
  82. {
  83. }
  84. }
  85. }