CipherCastCbc.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Security.Cryptography;
  6. using Renci.SshNet.Security.Cryptography;
  7. namespace Renci.SshNet.Security
  8. {
  9. /// <summary>
  10. /// Represents base class for CAST-CBC based encryption.
  11. /// </summary>
  12. public abstract class CipherCastCbc : Cipher
  13. {
  14. private readonly int _keySize;
  15. /// <summary>
  16. /// Gets or sets the key size, in bits, of the secret key used by the cipher.
  17. /// </summary>
  18. /// <value>
  19. /// The key size, in bits.
  20. /// </value>
  21. public override int KeySize
  22. {
  23. get
  24. {
  25. return this._keySize;
  26. }
  27. }
  28. /// <summary>
  29. /// Gets or sets the block size, in bits, of the cipher operation.
  30. /// </summary>
  31. /// <value>
  32. /// The block size, in bits.
  33. /// </value>
  34. public override int BlockSize
  35. {
  36. get
  37. {
  38. return 8;
  39. }
  40. }
  41. /// <summary>
  42. /// Initializes a new instance of the <see cref="CipherCastCbc"/> class.
  43. /// </summary>
  44. /// <param name="keySize">Size of the key.</param>
  45. public CipherCastCbc(int keySize)
  46. {
  47. this._keySize = keySize;
  48. }
  49. /// <summary>
  50. /// Creates the encryptor.
  51. /// </summary>
  52. /// <returns></returns>
  53. protected override ModeBase CreateEncryptor()
  54. {
  55. return new CbcMode(new CastCipher(this.Key.Take(this.KeySize / 8).ToArray(), this.Vector.Take(this.BlockSize).ToArray()));
  56. }
  57. /// <summary>
  58. /// Creates the decryptor.
  59. /// </summary>
  60. /// <returns></returns>
  61. protected override ModeBase CreateDecryptor()
  62. {
  63. return new CbcMode(new CastCipher(this.Key.Take(this.KeySize / 8).ToArray(), this.Vector.Take(this.BlockSize).ToArray()));
  64. }
  65. }
  66. /// <summary>
  67. /// Represents class for CAST 128 CBC based encryption.
  68. /// </summary>
  69. public class CipherCast128Cbc : CipherCastCbc
  70. {
  71. /// <summary>
  72. /// Gets algorithm name.
  73. /// </summary>
  74. public override string Name
  75. {
  76. get { return "cast128-cbc"; }
  77. }
  78. /// <summary>
  79. /// Initializes a new instance of the <see cref="CipherCast128Cbc"/> class.
  80. /// </summary>
  81. public CipherCast128Cbc()
  82. : base(128)
  83. {
  84. }
  85. }
  86. }