The next generation of the Teknik Services. Written in ASP.NET. Fork for blog tags.
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.

AES.cs 4.5KB

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