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.

Crypto.cs 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. using System;
  15. namespace Teknik.Helpers
  16. {
  17. public class SHA384
  18. {
  19. public static string Hash(string key, string value)
  20. {
  21. byte[] keyBytes = Encoding.ASCII.GetBytes(key);
  22. byte[] data = Encoding.ASCII.GetBytes(value);
  23. byte[] result = new HMAC2(HashFactories.SHA384, keyBytes).ComputeHash(data);
  24. return Encoding.ASCII.GetString(result);
  25. }
  26. }
  27. public class SHA256
  28. {
  29. public static string Hash(string value, string salt1, string salt2)
  30. {
  31. SHA256Managed hash = new SHA256Managed();
  32. SHA1 sha1 = new SHA1Managed();
  33. // gen salt2 hash
  34. byte[] dataSalt2 = Encoding.UTF8.GetBytes(salt2);
  35. byte[] salt2Bytes = hash.ComputeHash(dataSalt2);
  36. string salt2Str = string.Empty;
  37. foreach (byte x in salt2Bytes)
  38. {
  39. salt2Str += String.Format("{0:x2}", x);
  40. }
  41. string dataStr = salt1 + value + salt2Str;
  42. byte[] dataStrBytes = Encoding.UTF8.GetBytes(dataStr);
  43. byte[] shaBytes = sha1.ComputeHash(dataStrBytes);
  44. string sha1Str = string.Empty;
  45. foreach (byte x in shaBytes)
  46. {
  47. sha1Str += String.Format("{0:x2}", x);
  48. }
  49. byte[] sha1Bytes = Encoding.UTF8.GetBytes(sha1Str);
  50. byte[] valueBytes = hash.ComputeHash(sha1Bytes);
  51. string hashString = string.Empty;
  52. foreach (byte x in valueBytes)
  53. {
  54. hashString += String.Format("{0:x2}", x);
  55. }
  56. return hashString;
  57. }
  58. }
  59. public class AES
  60. {
  61. public static byte[] Decrypt(byte[] data, byte[] key, byte[] iv)
  62. {
  63. return Decrypt(data, key, iv, "CTR", "NoPadding");
  64. }
  65. public static byte[] Decrypt(byte[] data, string key, string iv)
  66. {
  67. byte[] keyBytes = Encoding.UTF8.GetBytes(key);
  68. byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
  69. return Decrypt(data, keyBytes, ivBytes, "CTR", "NoPadding");
  70. }
  71. public static byte[] DecryptCBC(byte[] data, string key, string iv)
  72. {
  73. byte[] keyBytes = Encoding.UTF8.GetBytes(key);
  74. byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
  75. return Decrypt(data, keyBytes, ivBytes, "CBC", "PKCS5PADDING");
  76. }
  77. public static byte[] Decrypt(byte[] data, byte[] key, byte[] iv, string mode, string padding)
  78. {
  79. IBufferedCipher cipher = CipherUtilities.GetCipher("AES/" + mode + "/" + padding);
  80. cipher.Init(false, new ParametersWithIV(ParameterUtilities.CreateKeyParameter("AES", key), iv));
  81. return cipher.DoFinal(data);
  82. }
  83. public static byte[] Encrypt(byte[] data, byte[] key, byte[] iv)
  84. {
  85. return Encrypt(data, key, iv, "CTR", "NoPadding");
  86. }
  87. public static byte[] Encrypt(byte[] data, string key, string iv)
  88. {
  89. byte[] keyBytes = Encoding.UTF8.GetBytes(key);
  90. byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
  91. return Encrypt(data, keyBytes, ivBytes, "CTR", "NoPadding");
  92. }
  93. public static byte[] EncryptCBC(byte[] data, string key, string iv)
  94. {
  95. byte[] keyBytes = Encoding.UTF8.GetBytes(key);
  96. byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
  97. return Encrypt(data, keyBytes, ivBytes, "CBC", "PKCS5PADDING");
  98. }
  99. public static byte[] Encrypt(byte[] data, byte[] key, byte[] iv, string mode, string padding)
  100. {
  101. IBufferedCipher cipher = CipherUtilities.GetCipher("AES/" + mode + "/" + padding);
  102. cipher.Init(true, new ParametersWithIV(ParameterUtilities.CreateKeyParameter("AES", key), iv));
  103. return cipher.DoFinal(data);
  104. }
  105. public static byte[] CreateKey(string password, string iv, int keySize = 256)
  106. {
  107. byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
  108. return CreateKey(password, ivBytes, keySize);
  109. }
  110. public static byte[] CreateKey(string password, byte[] iv, int keySize = 256)
  111. {
  112. const int Iterations = 300;
  113. var keyGenerator = new Rfc2898DeriveBytes(password, iv, Iterations);
  114. return keyGenerator.GetBytes(keySize / 8);
  115. }
  116. }
  117. }