The next generation of the Teknik Services. Written in ASP.NET. Fork for blog tags.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StreamHelper.cs 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Teknik.Utilities.Cryptography;
  9. namespace Teknik.Utilities
  10. {
  11. public class AESCryptoStream : Stream
  12. {
  13. private Stream _Inner;
  14. private CounterModeCryptoTransform _Cipher;
  15. public AESCryptoStream(Stream stream, bool encrypt, byte[] key, byte[] iv, string mode, string padding)
  16. {
  17. _Inner = stream;
  18. // Create the Aes Cipher
  19. AesCounterMode aes = new AesCounterMode(iv);
  20. if (encrypt)
  21. {
  22. _Cipher = (CounterModeCryptoTransform)aes.CreateEncryptor(key, iv); // Encrypt
  23. }
  24. else
  25. {
  26. _Cipher = (CounterModeCryptoTransform)aes.CreateDecryptor(key, iv); // Decrypt
  27. }
  28. // Sync the counter
  29. SyncCounter();
  30. }
  31. public override int Read(byte[] buffer, int offset, int count)
  32. {
  33. if (_Inner != null && CanRead)
  34. {
  35. byte[] readBuf = new byte[count];
  36. int processed = 0;
  37. // Read the data from the stream
  38. int bytesRead = _Inner.Read(readBuf, 0, count);
  39. if (bytesRead > 0)
  40. {
  41. // Process the
  42. processed = _Cipher.TransformBlock(readBuf, 0, bytesRead, buffer, 0);
  43. }
  44. // Do we have more?
  45. if (processed < bytesRead)
  46. {
  47. // Finalize the cipher
  48. byte[] finalBuf = _Cipher.TransformFinalBlock(readBuf, processed, bytesRead);
  49. finalBuf.CopyTo(buffer, processed);
  50. processed += finalBuf.Length;
  51. }
  52. return processed;
  53. }
  54. return -1;
  55. }
  56. public override void Write(byte[] buffer, int offset, int count)
  57. {
  58. if (_Inner != null && CanWrite)
  59. {
  60. // Process the cipher
  61. byte[] output = new byte[count];
  62. //int processed = _Cipher.ProcessBytes(buffer, offset, count, output, 0);
  63. // Finalize the cipher
  64. //AES.FinalizeCipherBlock(_Cipher, output, processed);
  65. _Inner.Write(output, 0, count);
  66. }
  67. }
  68. public override bool CanRead
  69. {
  70. get
  71. {
  72. if (_Inner != null)
  73. {
  74. return _Inner.CanRead;
  75. }
  76. return false;
  77. }
  78. }
  79. public override bool CanSeek
  80. {
  81. get
  82. {
  83. if (_Inner != null)
  84. {
  85. return _Inner.CanSeek;
  86. }
  87. return false;
  88. }
  89. }
  90. public override bool CanWrite
  91. {
  92. get
  93. {
  94. if (_Inner != null)
  95. {
  96. return _Inner.CanWrite;
  97. }
  98. return false;
  99. }
  100. }
  101. public override long Length
  102. {
  103. get
  104. {
  105. if (_Inner != null)
  106. {
  107. return _Inner.Length;
  108. }
  109. return -1;
  110. }
  111. }
  112. public override long Position
  113. {
  114. get
  115. {
  116. if (_Inner != null)
  117. {
  118. return _Inner.Position;
  119. }
  120. return -1;
  121. }
  122. set
  123. {
  124. if (_Inner != null)
  125. {
  126. _Inner.Position = value;
  127. // Sync the counter
  128. SyncCounter();
  129. }
  130. }
  131. }
  132. public override void Flush()
  133. {
  134. if (_Inner != null)
  135. {
  136. _Inner.Flush();
  137. }
  138. }
  139. public override long Seek(long offset, SeekOrigin origin)
  140. {
  141. if (_Inner != null)
  142. {
  143. long newPos = _Inner.Seek(offset, origin);
  144. // Sync the counter
  145. SyncCounter();
  146. return newPos;
  147. }
  148. return -1;
  149. }
  150. public override void SetLength(long value)
  151. {
  152. if (_Inner != null)
  153. {
  154. _Inner.SetLength(value);
  155. }
  156. }
  157. private void SyncCounter()
  158. {
  159. if (_Cipher != null)
  160. {
  161. // Calculate the counter iterations and position needed
  162. int iterations = (int)Math.Floor(_Inner.Position / (decimal)_Cipher.InputBlockSize);
  163. int counterPos = (int)(_Inner.Position % _Cipher.InputBlockSize);
  164. // Are we out of sync with the cipher?
  165. if (_Cipher.Iterations != iterations || _Cipher.CounterPosition != counterPos)
  166. {
  167. // Reset the current counter
  168. _Cipher.ResetCounter();
  169. // Iterate the counter to the current position
  170. for (int i = 0; i < iterations; i++)
  171. {
  172. _Cipher.IncrementCounter();
  173. }
  174. // Encrypt the counter
  175. _Cipher.EncryptCounter();
  176. // Set the current position of the counter
  177. _Cipher.CounterPosition = counterPos;
  178. // Increment the counter for the next time
  179. _Cipher.IncrementCounter();
  180. }
  181. }
  182. }
  183. }
  184. }