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 6.3KB

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