The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Aes128CFB.cs 3.3KB

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