CipherDES.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Security.Cryptography;
  6. namespace Renci.SshNet.Security
  7. {
  8. /// <summary>
  9. /// Represents DES-CBC encryption.
  10. /// </summary>
  11. public class CipherDES : Cipher
  12. {
  13. private SymmetricAlgorithm _algorithm;
  14. private ICryptoTransform _encryptor;
  15. private ICryptoTransform _decryptor;
  16. /// <summary>
  17. /// Gets algorithm name.
  18. /// </summary>
  19. public override string Name
  20. {
  21. get { return "3des-cbc"; }
  22. }
  23. /// <summary>
  24. /// Gets or sets the key size, in bits, of the secret key used by the cipher.
  25. /// </summary>
  26. /// <value>
  27. /// The key size, in bits.
  28. /// </value>
  29. public override int KeySize
  30. {
  31. get
  32. {
  33. return this._algorithm.KeySize;
  34. }
  35. }
  36. /// <summary>
  37. /// Gets or sets the block size, in bits, of the cipher operation.
  38. /// </summary>
  39. /// <value>
  40. /// The block size, in bits.
  41. /// </value>
  42. public override int BlockSize
  43. {
  44. get
  45. {
  46. return this._algorithm.BlockSize / 8;
  47. }
  48. }
  49. /// <summary>
  50. /// Initializes a new instance of the <see cref="CipherDES"/> class.
  51. /// </summary>
  52. public CipherDES()
  53. {
  54. this._algorithm = new System.Security.Cryptography.DESCryptoServiceProvider();
  55. this._algorithm.Mode = System.Security.Cryptography.CipherMode.CBC;
  56. this._algorithm.Padding = System.Security.Cryptography.PaddingMode.None;
  57. }
  58. /// <summary>
  59. /// Encrypts the specified data.
  60. /// </summary>
  61. /// <param name="data">The data.</param>
  62. /// <returns>
  63. /// Encrypted data
  64. /// </returns>
  65. public override byte[] Encrypt(byte[] data)
  66. {
  67. if (this._encryptor == null)
  68. {
  69. this._encryptor = this._algorithm.CreateEncryptor(this.Key.Take(this.KeySize / 8).ToArray(), this.Vector.Take(this.BlockSize).ToArray());
  70. }
  71. var output = new byte[data.Length];
  72. var writtenBytes = this._encryptor.TransformBlock(data, 0, data.Length, output, 0);
  73. if (writtenBytes < data.Length)
  74. {
  75. throw new InvalidOperationException("Encryption error.");
  76. }
  77. return output;
  78. }
  79. /// <summary>
  80. /// Decrypts the specified data.
  81. /// </summary>
  82. /// <param name="data">The data.</param>
  83. /// <returns>
  84. /// Decrypted data
  85. /// </returns>
  86. public override byte[] Decrypt(byte[] data)
  87. {
  88. if (this._decryptor == null)
  89. {
  90. this._decryptor = this._algorithm.CreateDecryptor(this.Key.Take(this.KeySize / 8).ToArray(), this.Vector.Take(this.BlockSize).ToArray());
  91. }
  92. var output = new byte[data.Length];
  93. var writtenBytes = this._decryptor.TransformBlock(data, 0, data.Length, output, 0);
  94. if (writtenBytes < data.Length)
  95. {
  96. throw new InvalidOperationException("Encryption error.");
  97. }
  98. return output;
  99. }
  100. private bool _isDisposed = false;
  101. /// <summary>
  102. /// Releases unmanaged and - optionally - managed resources
  103. /// </summary>
  104. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  105. protected override void Dispose(bool disposing)
  106. {
  107. // Check to see if Dispose has already been called.
  108. if (!this._isDisposed)
  109. {
  110. // If disposing equals true, dispose all managed
  111. // and unmanaged resources.
  112. if (disposing)
  113. {
  114. // Dispose managed resources.
  115. if (this._algorithm != null)
  116. {
  117. this._algorithm.Dispose();
  118. this._algorithm = null;
  119. }
  120. }
  121. // Note disposing has been done.
  122. this._isDisposed = true;
  123. }
  124. base.Dispose(disposing);
  125. }
  126. }
  127. }