The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System.Text;
  2. using SecurityDriven.Inferno.Hash;
  3. using SecurityDriven.Inferno.Mac;
  4. using System.IO;
  5. using System.Security.Cryptography;
  6. using Teknik.Configuration;
  7. using Org.BouncyCastle.Crypto;
  8. using Org.BouncyCastle.Crypto.Engines;
  9. using Org.BouncyCastle.Crypto.Modes;
  10. using Org.BouncyCastle.Crypto.Paddings;
  11. using Org.BouncyCastle.Crypto.Parameters;
  12. using Org.BouncyCastle.Security;
  13. using Org.BouncyCastle.Utilities.Encoders;
  14. namespace Teknik.Helpers
  15. {
  16. public class SHA384
  17. {
  18. public static string Hash(string key, string value)
  19. {
  20. byte[] keyBytes = Encoding.ASCII.GetBytes(key);
  21. byte[] data = Encoding.ASCII.GetBytes(value);
  22. byte[] result = new HMAC2(HashFactories.SHA384, keyBytes).ComputeHash(data);
  23. return Encoding.ASCII.GetString(result);
  24. }
  25. }
  26. public class AES
  27. {
  28. public static byte[] Decrypt(byte[] data, string key, string iv)
  29. {
  30. byte[] keyBytes = Encoding.UTF8.GetBytes(key);
  31. byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
  32. return Decrypt(data, keyBytes, ivBytes);
  33. }
  34. public static byte[] Decrypt(byte[] data, byte[] key, byte[] iv)
  35. {
  36. IBufferedCipher cipher = CipherUtilities.GetCipher("AES/CTR/NoPadding");
  37. cipher.Init(false, new ParametersWithIV(ParameterUtilities.CreateKeyParameter("AES", key), iv));
  38. return cipher.DoFinal(data);
  39. }
  40. public static byte[] Encrypt(byte[] data, string key, string iv)
  41. {
  42. byte[] keyBytes = Encoding.UTF8.GetBytes(key);
  43. byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
  44. return Encrypt(data, keyBytes, ivBytes);
  45. }
  46. public static byte[] Encrypt(byte[] data, byte[] key, byte[] iv)
  47. {
  48. IBufferedCipher cipher = CipherUtilities.GetCipher("AES/CTR/NoPadding");
  49. cipher.Init(true, new ParametersWithIV(ParameterUtilities.CreateKeyParameter("AES", key), iv));
  50. return cipher.DoFinal(data);
  51. }
  52. }
  53. }