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.

Aes128CFB.cs 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 static class Aes128CFB
  9. {
  10. public static byte[] Encrypt(byte[] text, byte[] key)
  11. {
  12. int blockSize = 128;
  13. int keySize = 128;
  14. // Encode the text
  15. string textEnc = Convert.ToBase64String(text);
  16. byte[] textBytes = Encoding.UTF8.GetBytes(textEnc);
  17. // cipherArray
  18. int cipherLen = (blockSize / 8) + textEnc.Length;
  19. byte[] cipherText = new byte[cipherLen];
  20. Array.Clear(cipherText, 0, cipherLen);
  21. // Create the IV needed for this operation
  22. string ivStr = StringHelper.RandomString(blockSize / 8);
  23. byte[] ivBytes = Encoding.UTF8.GetBytes(ivStr);
  24. // copy IV to the cipher text start
  25. ivBytes.CopyTo(cipherText, 0);
  26. // Process the cipher
  27. ProcessCipher(true, textBytes, key, ivBytes, blockSize, keySize, ref cipherText, blockSize / 8);
  28. return cipherText;
  29. }
  30. public static byte[] Decrypt(byte[] text, byte[] key)
  31. {
  32. int blockSize = 128;
  33. int keySize = 128;
  34. // Grab the IV and encrypted text from the original text
  35. byte[] ivBytes = new byte[blockSize / 8];
  36. byte[] encText = new byte[text.Length - (blockSize / 8)];
  37. byte[] output = new byte[text.Length - (blockSize / 8)];
  38. text.Take(blockSize / 8).ToArray().CopyTo(ivBytes, 0);
  39. text.Skip(blockSize / 8).ToArray().CopyTo(encText, 0);
  40. // Pad the text for decryption
  41. ByteHelper.PadToMultipleOf(ref encText, 16);
  42. // Process the cipher
  43. ProcessCipher(false, encText, key, ivBytes, blockSize, keySize, ref output, 0);
  44. string encodedText = Encoding.UTF8.GetString(output);
  45. return Convert.FromBase64String(encodedText);
  46. }
  47. public static void ProcessCipher(bool encrypt, byte[] text, byte[] key, byte[] iv, int blockSize, int keySize, ref byte[] output, int offset)
  48. {
  49. using (var cipher = new RijndaelManaged())
  50. {
  51. cipher.BlockSize = blockSize;
  52. cipher.KeySize = keySize;
  53. cipher.Mode = CipherMode.CFB;
  54. cipher.FeedbackSize = 128;
  55. cipher.Padding = PaddingMode.Zeros;
  56. cipher.Key = key;
  57. cipher.IV = iv;
  58. using (var encryptor = (encrypt) ? cipher.CreateEncryptor() : cipher.CreateDecryptor())
  59. using (MemoryStream ms = new MemoryStream())
  60. using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
  61. using (var bw = new BinaryWriter(cs, Encoding.UTF8))
  62. {
  63. bw.Write(text);
  64. bw.Close();
  65. byte[] textBytes = ms.ToArray();
  66. for (int i = 0; i < output.Length - offset; i++)
  67. {
  68. output[i + offset] = textBytes[i];
  69. }
  70. }
  71. }
  72. }
  73. }
  74. }