The next generation of the Teknik Services. Written in ASP.NET.
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 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using Org.BouncyCastle.Crypto;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace Teknik.Utilities
  9. {
  10. public class AESCryptoStream : Stream
  11. {
  12. private Stream _Inner;
  13. private IBufferedCipher _Cipher;
  14. public AESCryptoStream(Stream stream, bool encrypt, byte[] key, byte[] iv, string mode, string padding)
  15. {
  16. _Inner = stream;
  17. _Cipher = AES.CreateCipher(encrypt, key, iv, mode, padding);
  18. }
  19. public override int Read(byte[] buffer, int offset, int count)
  20. {
  21. if (_Inner != null && CanRead)
  22. {
  23. int bytesRead = 0;
  24. // Process the cipher
  25. int processed = AES.ProcessCipherBlock(_Cipher, _Inner, count, buffer, offset, out bytesRead);
  26. if (processed < bytesRead)
  27. {
  28. // Finalize the cipher
  29. processed += AES.FinalizeCipherBlock(_Cipher, buffer, processed + offset);
  30. }
  31. return bytesRead;
  32. }
  33. return -1;
  34. }
  35. public override void Write(byte[] buffer, int offset, int count)
  36. {
  37. if (_Inner != null && CanWrite)
  38. {
  39. // Process the cipher
  40. byte[] output = new byte[count];
  41. int processed = _Cipher.ProcessBytes(buffer, offset, count, output, 0);
  42. // Finalize the cipher
  43. AES.FinalizeCipherBlock(_Cipher, output, processed);
  44. _Inner.Write(output, 0, count);
  45. }
  46. }
  47. public override bool CanRead
  48. {
  49. get
  50. {
  51. if (_Inner != null)
  52. {
  53. return _Inner.CanRead;
  54. }
  55. return false;
  56. }
  57. }
  58. public override bool CanSeek
  59. {
  60. get
  61. {
  62. if (_Inner != null)
  63. {
  64. return _Inner.CanSeek;
  65. }
  66. return false;
  67. }
  68. }
  69. public override bool CanWrite
  70. {
  71. get
  72. {
  73. if (_Inner != null)
  74. {
  75. return _Inner.CanWrite;
  76. }
  77. return false;
  78. }
  79. }
  80. public override long Length
  81. {
  82. get
  83. {
  84. if (_Inner != null)
  85. {
  86. return _Inner.Length;
  87. }
  88. return -1;
  89. }
  90. }
  91. public override long Position
  92. {
  93. get
  94. {
  95. if (_Inner != null)
  96. {
  97. return _Inner.Position;
  98. }
  99. return -1;
  100. }
  101. set
  102. {
  103. if (_Inner != null)
  104. {
  105. _Inner.Position = value;
  106. }
  107. }
  108. }
  109. public override void Flush()
  110. {
  111. if (_Inner != null)
  112. {
  113. _Inner.Flush();
  114. }
  115. }
  116. public override long Seek(long offset, SeekOrigin origin)
  117. {
  118. if (_Inner != null)
  119. {
  120. return _Inner.Seek(offset, origin);
  121. }
  122. return -1;
  123. }
  124. public override void SetLength(long value)
  125. {
  126. if (_Inner != null)
  127. {
  128. _Inner.SetLength(value);
  129. }
  130. }
  131. }
  132. }