CipherAES128.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Cryptography;
  5. namespace Renci.SshClient.Security
  6. {
  7. internal class CipherAES128 : Cipher, IDisposable
  8. {
  9. private SymmetricAlgorithm _algorithm;
  10. private ICryptoTransform _encryptor;
  11. private ICryptoTransform _decryptor;
  12. public override string Name
  13. {
  14. get { return "aes128-cbc"; }
  15. }
  16. public override int KeySize
  17. {
  18. get
  19. {
  20. return this._algorithm.KeySize;
  21. }
  22. }
  23. public override int BlockSize
  24. {
  25. get
  26. {
  27. return this._algorithm.BlockSize / 8;
  28. }
  29. }
  30. public CipherAES128()
  31. {
  32. this._algorithm = new System.Security.Cryptography.RijndaelManaged();
  33. this._algorithm.Mode = System.Security.Cryptography.CipherMode.CBC;
  34. this._algorithm.Padding = System.Security.Cryptography.PaddingMode.None;
  35. }
  36. public override IEnumerable<byte> Encrypt(IEnumerable<byte> data)
  37. {
  38. if (this._encryptor == null)
  39. {
  40. this._encryptor = this._algorithm.CreateEncryptor(this.Key.Take(this.KeySize / 8).ToArray(), this.Vector.Take(this.BlockSize).ToArray());
  41. }
  42. var input = data.ToArray();
  43. var output = new byte[input.Length];
  44. var writtenBytes = this._encryptor.TransformBlock(input, 0, input.Length, output, 0);
  45. if (writtenBytes < input.Length)
  46. {
  47. throw new InvalidOperationException("Encryption error.");
  48. }
  49. return output;
  50. }
  51. public override IEnumerable<byte> Decrypt(IEnumerable<byte> data)
  52. {
  53. if (this._decryptor == null)
  54. {
  55. this._decryptor = this._algorithm.CreateDecryptor(this.Key.Take(this.KeySize / 8).ToArray(), this.Vector.Take(this.BlockSize).ToArray());
  56. }
  57. var input = data.ToArray();
  58. var output = new byte[input.Length];
  59. var writtenBytes = this._decryptor.TransformBlock(input, 0, input.Length, output, 0);
  60. if (writtenBytes < input.Length)
  61. {
  62. throw new InvalidOperationException("Encryption error.");
  63. }
  64. return output;
  65. }
  66. #region IDisposable Members
  67. private bool disposed = false;
  68. public void Dispose()
  69. {
  70. Dispose(true);
  71. GC.SuppressFinalize(this);
  72. }
  73. private void Dispose(bool disposing)
  74. {
  75. // Check to see if Dispose has already been called.
  76. if (!this.disposed)
  77. {
  78. // If disposing equals true, dispose all managed
  79. // and unmanaged resources.
  80. if (disposing)
  81. {
  82. // Dispose managed resources.
  83. if (this._algorithm != null)
  84. {
  85. this._algorithm.Dispose();
  86. }
  87. }
  88. // Note disposing has been done.
  89. disposed = true;
  90. }
  91. }
  92. ~CipherAES128()
  93. {
  94. // Do not re-create Dispose clean-up code here.
  95. // Calling Dispose(false) is optimal in terms of
  96. // readability and maintainability.
  97. Dispose(false);
  98. }
  99. #endregion
  100. }
  101. }