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 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 Org.BouncyCastle.Bcpg.OpenPgp;
  15. using Org.BouncyCastle.Utilities.IO;
  16. using System;
  17. using System.Collections.Generic;
  18. namespace Teknik.Helpers
  19. {
  20. public class MD5
  21. {
  22. public static string Hash(string value)
  23. {
  24. byte[] valBytes = Encoding.ASCII.GetBytes(value);
  25. System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
  26. byte[] hashBytes = md5.ComputeHash(valBytes);
  27. StringBuilder sBuilder = new StringBuilder();
  28. // Loop through each byte of the hashed data
  29. // and format each one as a hexadecimal string.
  30. for (int i = 0; i < hashBytes.Length; i++)
  31. {
  32. sBuilder.Append(hashBytes[i].ToString("x2"));
  33. }
  34. // Return the hexadecimal string.
  35. return sBuilder.ToString();
  36. }
  37. }
  38. public class SHA384
  39. {
  40. public static string Hash(string key, string value)
  41. {
  42. byte[] keyBytes = Encoding.ASCII.GetBytes(key);
  43. byte[] data = Encoding.ASCII.GetBytes(value);
  44. byte[] result = new HMAC2(HashFactories.SHA384, keyBytes).ComputeHash(data);
  45. return Encoding.ASCII.GetString(result);
  46. }
  47. }
  48. public class SHA256
  49. {
  50. public static string Hash(string value, string salt1, string salt2)
  51. {
  52. SHA256Managed hash = new SHA256Managed();
  53. SHA1 sha1 = new SHA1Managed();
  54. // gen salt2 hash
  55. byte[] dataSalt2 = Encoding.UTF8.GetBytes(salt2);
  56. byte[] salt2Bytes = hash.ComputeHash(dataSalt2);
  57. string salt2Str = string.Empty;
  58. foreach (byte x in salt2Bytes)
  59. {
  60. salt2Str += String.Format("{0:x2}", x);
  61. }
  62. string dataStr = salt1 + value + salt2Str;
  63. byte[] dataStrBytes = Encoding.UTF8.GetBytes(dataStr);
  64. byte[] shaBytes = sha1.ComputeHash(dataStrBytes);
  65. string sha1Str = string.Empty;
  66. foreach (byte x in shaBytes)
  67. {
  68. sha1Str += String.Format("{0:x2}", x);
  69. }
  70. byte[] sha1Bytes = Encoding.UTF8.GetBytes(sha1Str);
  71. byte[] valueBytes = hash.ComputeHash(sha1Bytes);
  72. string hashString = string.Empty;
  73. foreach (byte x in valueBytes)
  74. {
  75. hashString += String.Format("{0:x2}", x);
  76. }
  77. return hashString;
  78. }
  79. }
  80. public class AES
  81. {
  82. public static byte[] Decrypt(byte[] data, byte[] key, byte[] iv)
  83. {
  84. return Decrypt(data, key, iv, "CTR", "NoPadding");
  85. }
  86. public static byte[] Decrypt(byte[] data, string key, string iv)
  87. {
  88. byte[] keyBytes = Encoding.UTF8.GetBytes(key);
  89. byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
  90. return Decrypt(data, keyBytes, ivBytes, "CTR", "NoPadding");
  91. }
  92. public static byte[] DecryptCBC(byte[] data, string key, string iv)
  93. {
  94. byte[] keyBytes = Encoding.UTF8.GetBytes(key);
  95. byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
  96. return Decrypt(data, keyBytes, ivBytes, "CBC", "PKCS5PADDING");
  97. }
  98. public static byte[] Decrypt(byte[] data, byte[] key, byte[] iv, string mode, string padding)
  99. {
  100. IBufferedCipher cipher = CipherUtilities.GetCipher("AES/" + mode + "/" + padding);
  101. cipher.Init(false, new ParametersWithIV(ParameterUtilities.CreateKeyParameter("AES", key), iv));
  102. return cipher.DoFinal(data);
  103. }
  104. public static byte[] Encrypt(byte[] data, byte[] key, byte[] iv)
  105. {
  106. return Encrypt(data, key, iv, "CTR", "NoPadding");
  107. }
  108. public static byte[] Encrypt(byte[] data, string key, string iv)
  109. {
  110. byte[] keyBytes = Encoding.UTF8.GetBytes(key);
  111. byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
  112. return Encrypt(data, keyBytes, ivBytes, "CTR", "NoPadding");
  113. }
  114. public static byte[] EncryptCBC(byte[] data, string key, string iv)
  115. {
  116. byte[] keyBytes = Encoding.UTF8.GetBytes(key);
  117. byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
  118. return Encrypt(data, keyBytes, ivBytes, "CBC", "PKCS5PADDING");
  119. }
  120. public static byte[] Encrypt(byte[] data, byte[] key, byte[] iv, string mode, string padding)
  121. {
  122. IBufferedCipher cipher = CipherUtilities.GetCipher("AES/" + mode + "/" + padding);
  123. cipher.Init(true, new ParametersWithIV(ParameterUtilities.CreateKeyParameter("AES", key), iv));
  124. return cipher.DoFinal(data);
  125. }
  126. public static byte[] CreateKey(string password, string iv, int keySize = 256)
  127. {
  128. byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
  129. return CreateKey(password, ivBytes, keySize);
  130. }
  131. public static byte[] CreateKey(string password, byte[] iv, int keySize = 256)
  132. {
  133. const int Iterations = 300;
  134. var keyGenerator = new Rfc2898DeriveBytes(password, iv, Iterations);
  135. return keyGenerator.GetBytes(keySize / 8);
  136. }
  137. }
  138. public static class PGP
  139. {
  140. public static bool IsPublicKey(string key)
  141. {
  142. bool isValid = false;
  143. try
  144. {
  145. byte[] byteArray = Encoding.ASCII.GetBytes(key);
  146. using (MemoryStream stream = new MemoryStream(byteArray))
  147. {
  148. using (Stream decoderStream = PgpUtilities.GetDecoderStream(stream))
  149. {
  150. PgpPublicKeyRingBundle publicKeyBundle = new PgpPublicKeyRingBundle(decoderStream);
  151. PgpPublicKey foundKey = GetFirstPublicKey(publicKeyBundle);
  152. if (foundKey != null)
  153. {
  154. isValid = true;
  155. }
  156. }
  157. }
  158. }
  159. catch (Exception ex)
  160. {
  161. isValid = false;
  162. }
  163. return isValid;
  164. }
  165. public static string GetFingerprint(string key)
  166. {
  167. string hexString = string.Empty;
  168. byte[] byteArray = Encoding.ASCII.GetBytes(key);
  169. using (MemoryStream stream = new MemoryStream(byteArray))
  170. {
  171. using (Stream decoderStream = PgpUtilities.GetDecoderStream(stream))
  172. {
  173. PgpPublicKeyRingBundle publicKeyBundle = new PgpPublicKeyRingBundle(decoderStream);
  174. PgpPublicKey foundKey = GetFirstPublicKey(publicKeyBundle);
  175. if (foundKey != null)
  176. {
  177. byte[] fing = foundKey.GetFingerprint();
  178. hexString = Hex.ToHexString(fing);
  179. }
  180. }
  181. }
  182. return hexString;
  183. }
  184. public static string GetFingerprint64(string key)
  185. {
  186. string fingerprint = GetFingerprint(key);
  187. if (fingerprint.Length > 16)
  188. fingerprint = fingerprint.Substring(fingerprint.Length - 16);
  189. return fingerprint;
  190. }
  191. private static PgpPublicKey GetFirstPublicKey(PgpPublicKeyRingBundle publicKeyRingBundle)
  192. {
  193. foreach (PgpPublicKeyRing kRing in publicKeyRingBundle.GetKeyRings())
  194. {
  195. var keys = kRing.GetPublicKeys();
  196. foreach (var key in keys)
  197. {
  198. PgpPublicKey foundKey = (PgpPublicKey)key;
  199. //PgpPublicKey key = kRing.GetPublicKeys()
  200. //.Cast<PgpPublicKey>()
  201. // .Where(k => k.IsEncryptionKey)
  202. // .FirstOrDefault();
  203. if (foundKey != null && foundKey.IsEncryptionKey)
  204. return foundKey;
  205. }
  206. }
  207. return null;
  208. }
  209. }
  210. }