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.

AesCounterManaged.cs 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. namespace Teknik.Utilities.Cryptography
  7. {
  8. public class AesCounterManaged
  9. {
  10. public static byte[] Decrypt(byte[] data, string key, string iv)
  11. {
  12. byte[] keyBytes = Encoding.UTF8.GetBytes(key);
  13. byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
  14. return Decrypt(data, keyBytes, ivBytes);
  15. }
  16. public static byte[] Decrypt(byte[] data, byte[] key, byte[] iv)
  17. {
  18. using (MemoryStream stream = new MemoryStream(data))
  19. {
  20. return ProcessCipher(false, stream, 1024, key, iv);
  21. }
  22. }
  23. public static byte[] Encrypt(byte[] data, string key, string iv)
  24. {
  25. byte[] keyBytes = Encoding.UTF8.GetBytes(key);
  26. byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
  27. return Encrypt(data, keyBytes, ivBytes);
  28. }
  29. public static byte[] Encrypt(byte[] data, byte[] key, byte[] iv)
  30. {
  31. using (MemoryStream stream = new MemoryStream(data))
  32. {
  33. return ProcessCipher(true, stream, 1024, key, iv);
  34. }
  35. }
  36. public static byte[] ProcessCipher(bool encrypt, Stream input, int chunkSize, byte[] key, byte[] iv)
  37. {
  38. // Make sure the input stream is at the beginning
  39. input.Seek(0, SeekOrigin.Begin);
  40. AesCounterStream cryptoStream = new AesCounterStream(input, encrypt, key, iv);
  41. // Initialize variables
  42. byte[] output = new byte[input.Length];
  43. // Process the stream and save the bytes to the output
  44. int curByte = 0;
  45. int processedBytes = 0;
  46. byte[] buffer = new byte[chunkSize];
  47. int bytesRemaining = (int)input.Length;
  48. int bytesToRead = chunkSize;
  49. do
  50. {
  51. if (chunkSize > bytesRemaining)
  52. {
  53. bytesToRead = bytesRemaining;
  54. }
  55. processedBytes = cryptoStream.Read(buffer, 0, bytesToRead);
  56. if (processedBytes > 0)
  57. {
  58. buffer.Take(processedBytes).ToArray().CopyTo(output, curByte);
  59. // Clear the buffer
  60. Array.Clear(buffer, 0, chunkSize);
  61. }
  62. curByte += processedBytes;
  63. bytesRemaining -= processedBytes;
  64. }
  65. while (processedBytes > 0 && bytesRemaining > 0);
  66. return output;
  67. }
  68. public static void EncryptToFile(string filePath, Stream input, int chunkSize, byte[] key, byte[] iv)
  69. {
  70. // Make sure the input stream is at the beginning
  71. input.Seek(0, SeekOrigin.Begin);
  72. AesCounterStream cryptoStream = new AesCounterStream(input, true, key, iv);
  73. using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
  74. {
  75. int curByte = 0;
  76. int processedBytes = 0;
  77. byte[] buffer = new byte[chunkSize];
  78. int bytesRemaining = (int)input.Length;
  79. int bytesToRead = chunkSize;
  80. do
  81. {
  82. if (chunkSize > bytesRemaining)
  83. {
  84. bytesToRead = bytesRemaining;
  85. }
  86. processedBytes = cryptoStream.Read(buffer, 0, bytesToRead);
  87. if (processedBytes > 0)
  88. {
  89. fileStream.Write(buffer, 0, processedBytes);
  90. // Clear the buffer
  91. Array.Clear(buffer, 0, chunkSize);
  92. }
  93. curByte += processedBytes;
  94. bytesRemaining -= processedBytes;
  95. }
  96. while (processedBytes > 0 && bytesRemaining > 0);
  97. }
  98. }
  99. public static byte[] CreateKey(string password, string iv, int keySize = 256)
  100. {
  101. byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
  102. return CreateKey(password, ivBytes, keySize);
  103. }
  104. public static byte[] CreateKey(string password, byte[] iv, int keySize = 256)
  105. {
  106. const int Iterations = 5000;
  107. var keyGenerator = new Rfc2898DeriveBytes(password, iv, Iterations);
  108. return keyGenerator.GetBytes(keySize / 8);
  109. }
  110. }
  111. }