The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
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.

AesCounterStream.cs 6.2KB

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