The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ResponseHelper.cs 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. }
  42. catch (HttpException httpEx)
  43. {
  44. // If we lost connection, that's fine
  45. if (httpEx.ErrorCode == -2147023667)
  46. {
  47. // do nothing
  48. }
  49. else
  50. {
  51. throw httpEx;
  52. }
  53. }
  54. catch (Exception ex)
  55. {
  56. throw ex;
  57. }
  58. finally
  59. {
  60. // dispose of file stream
  61. stream.Dispose();
  62. }
  63. }
  64. public static void DecryptStreamToOutput(HttpResponseBase response, Stream stream, int length, byte[] key, byte[] iv, string mode, string padding, int chunkSize)
  65. {
  66. try
  67. {
  68. response.Flush();
  69. IBufferedCipher cipher = AES.CreateCipher(false, key, iv, mode, padding);
  70. int curByte = 0;
  71. int processedBytes = 0;
  72. byte[] buffer = new byte[chunkSize];
  73. int bytesRemaining = length;
  74. int bytesToRead = chunkSize;
  75. do
  76. {
  77. if (chunkSize > bytesRemaining)
  78. {
  79. bytesToRead = bytesRemaining;
  80. }
  81. processedBytes = AES.ProcessCipherBlock(cipher, stream, bytesToRead, buffer, 0);
  82. if (processedBytes > 0)
  83. {
  84. response.OutputStream.Write(buffer, 0, processedBytes);
  85. response.Flush();
  86. // Clear the buffer
  87. Array.Clear(buffer, 0, chunkSize);
  88. }
  89. curByte += processedBytes;
  90. bytesRemaining -= processedBytes;
  91. }
  92. while (processedBytes > 0 && bytesRemaining > 0);
  93. if (bytesRemaining > 0)
  94. {
  95. // Clear the buffer
  96. Array.Clear(buffer, 0, chunkSize);
  97. // Finalize processing of the cipher
  98. processedBytes = AES.FinalizeCipherBlock(cipher, buffer, 0);
  99. if (processedBytes > 0)
  100. {
  101. // We have bytes, lets write them to the output
  102. response.OutputStream.Write(buffer, 0, processedBytes);
  103. response.Flush();
  104. }
  105. }
  106. }
  107. catch (HttpException httpEx)
  108. {
  109. // If we lost connection, that's fine
  110. if (httpEx.ErrorCode == -2147023667)
  111. {
  112. // do nothing
  113. }
  114. else
  115. {
  116. throw httpEx;
  117. }
  118. }
  119. catch (Exception ex)
  120. {
  121. throw ex;
  122. }
  123. finally
  124. {
  125. // dispose of file stream
  126. stream.Dispose();
  127. }
  128. }
  129. }
  130. }