The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ResponseHelper.cs 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. using System.Web;
  9. namespace Teknik.Utilities
  10. {
  11. public static class ResponseHelper
  12. {
  13. public static void StreamToOutput(HttpResponseBase response, Stream stream, int length, int chunkSize)
  14. {
  15. try
  16. {
  17. response.Flush();
  18. int curByte = 0;
  19. int processedBytes = 0;
  20. byte[] buffer = new byte[chunkSize];
  21. int bytesRemaining = length;
  22. int bytesToRead = chunkSize;
  23. do
  24. {
  25. if (chunkSize > bytesRemaining)
  26. {
  27. bytesToRead = bytesRemaining;
  28. }
  29. processedBytes = stream.Read(buffer, 0, bytesToRead);
  30. if (processedBytes > 0)
  31. {
  32. response.OutputStream.Write(buffer, 0, processedBytes);
  33. response.Flush();
  34. // Clear the buffer
  35. Array.Clear(buffer, 0, chunkSize);
  36. }
  37. curByte += processedBytes;
  38. bytesRemaining -= processedBytes;
  39. }
  40. while (processedBytes > 0 && bytesRemaining > 0);
  41. if (bytesRemaining > 0)
  42. {
  43. // Clear the buffer
  44. Array.Clear(buffer, 0, chunkSize);
  45. // Finalize processing of the cipher
  46. processedBytes = stream.Read(buffer, 0, bytesToRead);
  47. if (processedBytes > 0)
  48. {
  49. // We have bytes, lets write them to the output
  50. response.OutputStream.Write(buffer, 0, processedBytes);
  51. response.Flush();
  52. }
  53. }
  54. }
  55. catch (HttpException httpEx)
  56. {
  57. // If we lost connection, that's fine
  58. if (httpEx.ErrorCode == -2147023667)
  59. {
  60. // do nothing
  61. }
  62. else
  63. {
  64. throw httpEx;
  65. }
  66. }
  67. catch (Exception ex)
  68. {
  69. throw ex;
  70. }
  71. finally
  72. {
  73. // dispose of file stream
  74. stream.Dispose();
  75. }
  76. }
  77. public static void DecryptStreamToOutput(HttpResponseBase response, Stream stream, int length, byte[] key, byte[] iv, string mode, string padding, int chunkSize)
  78. {
  79. try
  80. {
  81. response.Flush();
  82. IBufferedCipher cipher = AES.CreateCipher(false, key, iv, mode, padding);
  83. int curByte = 0;
  84. int processedBytes = 0;
  85. byte[] buffer = new byte[chunkSize];
  86. int bytesRemaining = length;
  87. int bytesToRead = chunkSize;
  88. do
  89. {
  90. if (chunkSize > bytesRemaining)
  91. {
  92. bytesToRead = bytesRemaining;
  93. }
  94. processedBytes = AES.ProcessCipherBlock(cipher, stream, bytesToRead, buffer, 0);
  95. if (processedBytes > 0)
  96. {
  97. response.OutputStream.Write(buffer, 0, processedBytes);
  98. response.Flush();
  99. // Clear the buffer
  100. Array.Clear(buffer, 0, chunkSize);
  101. }
  102. curByte += processedBytes;
  103. bytesRemaining -= processedBytes;
  104. }
  105. while (processedBytes > 0 && bytesRemaining > 0);
  106. if (bytesRemaining > 0)
  107. {
  108. // Clear the buffer
  109. Array.Clear(buffer, 0, chunkSize);
  110. // Finalize processing of the cipher
  111. processedBytes = AES.FinalizeCipherBlock(cipher, buffer, 0);
  112. if (processedBytes > 0)
  113. {
  114. // We have bytes, lets write them to the output
  115. response.OutputStream.Write(buffer, 0, processedBytes);
  116. response.Flush();
  117. }
  118. }
  119. }
  120. catch (HttpException httpEx)
  121. {
  122. // If we lost connection, that's fine
  123. if (httpEx.ErrorCode == -2147023667)
  124. {
  125. // do nothing
  126. }
  127. else
  128. {
  129. throw httpEx;
  130. }
  131. }
  132. catch (Exception ex)
  133. {
  134. throw ex;
  135. }
  136. finally
  137. {
  138. // dispose of file stream
  139. stream.Dispose();
  140. }
  141. }
  142. }
  143. }