The next generation of the Teknik Services. Written in ASP.NET. Fork for blog tags.
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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. public static string FileHash(string filename)
  38. {
  39. try
  40. {
  41. using (var md5 = System.Security.Cryptography.MD5.Create())
  42. {
  43. using (var stream = File.OpenRead(filename))
  44. {
  45. return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower();
  46. }
  47. }
  48. }
  49. catch (Exception)
  50. {
  51. return string.Empty;
  52. }
  53. }
  54. public static string DataHash(string data)
  55. {
  56. try
  57. {
  58. using (var md5 = System.Security.Cryptography.MD5.Create())
  59. {
  60. // convert string to stream
  61. byte[] byteArray = Encoding.UTF8.GetBytes(data);
  62. using (MemoryStream stream = new MemoryStream(byteArray))
  63. {
  64. return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower();
  65. }
  66. }
  67. }
  68. catch (Exception)
  69. {
  70. return string.Empty;
  71. }
  72. }
  73. }
  74. public class SHA384
  75. {
  76. public static byte[] Hash(string key, string value)
  77. {
  78. byte[] keyBytes = Encoding.UTF8.GetBytes(key);
  79. byte[] data = Encoding.UTF8.GetBytes(value);
  80. byte[] result = new HMAC2(HashFactories.SHA384, keyBytes).ComputeHash(data);
  81. return result;
  82. }
  83. }
  84. public class SHA256
  85. {
  86. public static string Hash(string value)
  87. {
  88. byte[] valueBytes = Encoding.Unicode.GetBytes(value);
  89. HashAlgorithm hash = new SHA256CryptoServiceProvider();
  90. byte[] hashBytes = hash.ComputeHash(valueBytes);
  91. return Convert.ToBase64String(hashBytes);
  92. }
  93. public static string Hash(string value, string salt1, string salt2)
  94. {
  95. SHA256Managed hash = new SHA256Managed();
  96. SHA1 sha1 = new SHA1Managed();
  97. // gen salt2 hash
  98. byte[] dataSalt2 = Encoding.UTF8.GetBytes(salt2);
  99. byte[] salt2Bytes = hash.ComputeHash(dataSalt2);
  100. string salt2Str = string.Empty;
  101. foreach (byte x in salt2Bytes)
  102. {
  103. salt2Str += String.Format("{0:x2}", x);
  104. }
  105. string dataStr = salt1 + value + salt2Str;
  106. byte[] dataStrBytes = Encoding.UTF8.GetBytes(dataStr);
  107. byte[] shaBytes = sha1.ComputeHash(dataStrBytes);
  108. string sha1Str = string.Empty;
  109. foreach (byte x in shaBytes)
  110. {
  111. sha1Str += String.Format("{0:x2}", x);
  112. }
  113. byte[] sha1Bytes = Encoding.UTF8.GetBytes(sha1Str);
  114. byte[] valueBytes = hash.ComputeHash(sha1Bytes);
  115. string hashString = string.Empty;
  116. foreach (byte x in valueBytes)
  117. {
  118. hashString += String.Format("{0:x2}", x);
  119. }
  120. return hashString;
  121. }
  122. public static System.Security.Cryptography.SHA256 CreateHashAlgorithm()
  123. {
  124. if (CryptoConfig.AllowOnlyFipsAlgorithms)
  125. {
  126. return new SHA256CryptoServiceProvider();
  127. }
  128. return new SHA256Managed();
  129. }
  130. }
  131. public class AES
  132. {
  133. public static byte[] Decrypt(byte[] data, byte[] key, byte[] iv)
  134. {
  135. return Decrypt(data, key, iv, "CTR", "NoPadding");
  136. }
  137. public static byte[] Decrypt(byte[] data, string key, string iv)
  138. {
  139. byte[] keyBytes = Encoding.UTF8.GetBytes(key);
  140. byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
  141. return Decrypt(data, keyBytes, ivBytes, "CTR", "NoPadding");
  142. }
  143. public static byte[] DecryptCBC(byte[] data, string key, string iv)
  144. {
  145. byte[] keyBytes = Encoding.UTF8.GetBytes(key);
  146. byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
  147. return Decrypt(data, keyBytes, ivBytes, "CBC", "PKCS5PADDING");
  148. }
  149. public static byte[] Decrypt(byte[] data, byte[] key, byte[] iv, string mode, string padding)
  150. {
  151. IBufferedCipher cipher = CipherUtilities.GetCipher("AES/" + mode + "/" + padding);
  152. cipher.Init(false, new ParametersWithIV(ParameterUtilities.CreateKeyParameter("AES", key), iv));
  153. return cipher.DoFinal(data);
  154. }
  155. public static byte[] Encrypt(byte[] data, byte[] key, byte[] iv)
  156. {
  157. return Encrypt(data, key, iv, "CTR", "NoPadding");
  158. }
  159. public static byte[] Encrypt(byte[] data, string key, string iv)
  160. {
  161. byte[] keyBytes = Encoding.UTF8.GetBytes(key);
  162. byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
  163. return Encrypt(data, keyBytes, ivBytes, "CTR", "NoPadding");
  164. }
  165. public static byte[] EncryptCBC(byte[] data, string key, string iv)
  166. {
  167. byte[] keyBytes = Encoding.UTF8.GetBytes(key);
  168. byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
  169. return Encrypt(data, keyBytes, ivBytes, "CBC", "PKCS5PADDING");
  170. }
  171. public static byte[] Encrypt(byte[] data, byte[] key, byte[] iv, string mode, string padding)
  172. {
  173. IBufferedCipher cipher = CipherUtilities.GetCipher("AES/" + mode + "/" + padding);
  174. cipher.Init(true, new ParametersWithIV(ParameterUtilities.CreateKeyParameter("AES", key), iv));
  175. return cipher.DoFinal(data);
  176. }
  177. public static byte[] CreateKey(string password, string iv, int keySize = 256)
  178. {
  179. byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
  180. return CreateKey(password, ivBytes, keySize);
  181. }
  182. public static byte[] CreateKey(string password, byte[] iv, int keySize = 256)
  183. {
  184. const int Iterations = 300;
  185. var keyGenerator = new Rfc2898DeriveBytes(password, iv, Iterations);
  186. return keyGenerator.GetBytes(keySize / 8);
  187. }
  188. }
  189. public static class PGP
  190. {
  191. public static bool IsPublicKey(string key)
  192. {
  193. bool isValid = false;
  194. try
  195. {
  196. byte[] byteArray = Encoding.ASCII.GetBytes(key);
  197. using (MemoryStream stream = new MemoryStream(byteArray))
  198. {
  199. using (Stream decoderStream = PgpUtilities.GetDecoderStream(stream))
  200. {
  201. PgpPublicKeyRingBundle publicKeyBundle = new PgpPublicKeyRingBundle(decoderStream);
  202. PgpPublicKey foundKey = GetFirstPublicKey(publicKeyBundle);
  203. if (foundKey != null)
  204. {
  205. isValid = true;
  206. }
  207. }
  208. }
  209. }
  210. catch (Exception ex)
  211. {
  212. isValid = false;
  213. }
  214. return isValid;
  215. }
  216. public static string GetFingerprint(string key)
  217. {
  218. string hexString = string.Empty;
  219. byte[] byteArray = Encoding.ASCII.GetBytes(key);
  220. using (MemoryStream stream = new MemoryStream(byteArray))
  221. {
  222. using (Stream decoderStream = PgpUtilities.GetDecoderStream(stream))
  223. {
  224. PgpPublicKeyRingBundle publicKeyBundle = new PgpPublicKeyRingBundle(decoderStream);
  225. PgpPublicKey foundKey = GetFirstPublicKey(publicKeyBundle);
  226. if (foundKey != null)
  227. {
  228. byte[] fing = foundKey.GetFingerprint();
  229. hexString = Hex.ToHexString(fing);
  230. }
  231. }
  232. }
  233. return hexString;
  234. }
  235. public static string GetFingerprint64(string key)
  236. {
  237. string fingerprint = GetFingerprint(key);
  238. if (fingerprint.Length > 16)
  239. fingerprint = fingerprint.Substring(fingerprint.Length - 16);
  240. return fingerprint;
  241. }
  242. private static PgpPublicKey GetFirstPublicKey(PgpPublicKeyRingBundle publicKeyRingBundle)
  243. {
  244. foreach (PgpPublicKeyRing kRing in publicKeyRingBundle.GetKeyRings())
  245. {
  246. var keys = kRing.GetPublicKeys();
  247. foreach (var key in keys)
  248. {
  249. PgpPublicKey foundKey = (PgpPublicKey)key;
  250. //PgpPublicKey key = kRing.GetPublicKeys()
  251. //.Cast<PgpPublicKey>()
  252. // .Where(k => k.IsEncryptionKey)
  253. // .FirstOrDefault();
  254. if (foundKey != null && foundKey.IsEncryptionKey)
  255. return foundKey;
  256. }
  257. }
  258. return null;
  259. }
  260. }
  261. }