The next generation of the Teknik Services. Written in ASP.NET.
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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 Org.BouncyCastle.Crypto;
  7. using Org.BouncyCastle.Crypto.Engines;
  8. using Org.BouncyCastle.Crypto.Modes;
  9. using Org.BouncyCastle.Crypto.Paddings;
  10. using Org.BouncyCastle.Crypto.Parameters;
  11. using Org.BouncyCastle.Security;
  12. using Org.BouncyCastle.Utilities.Encoders;
  13. using Org.BouncyCastle.Bcpg.OpenPgp;
  14. using Org.BouncyCastle.Utilities.IO;
  15. using System;
  16. using System.Collections.Generic;
  17. namespace Teknik.Utilities
  18. {
  19. public class MD5
  20. {
  21. public static string Hash(string value)
  22. {
  23. byte[] valBytes = Encoding.ASCII.GetBytes(value);
  24. System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
  25. byte[] hashBytes = md5.ComputeHash(valBytes);
  26. StringBuilder sBuilder = new StringBuilder();
  27. // Loop through each byte of the hashed data
  28. // and format each one as a hexadecimal string.
  29. for (int i = 0; i < hashBytes.Length; i++)
  30. {
  31. sBuilder.Append(hashBytes[i].ToString("x2"));
  32. }
  33. // Return the hexadecimal string.
  34. return sBuilder.ToString();
  35. }
  36. public static string FileHash(string filename)
  37. {
  38. try
  39. {
  40. using (var md5 = System.Security.Cryptography.MD5.Create())
  41. {
  42. using (var stream = File.OpenRead(filename))
  43. {
  44. return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower();
  45. }
  46. }
  47. }
  48. catch (Exception)
  49. {
  50. return string.Empty;
  51. }
  52. }
  53. public static string DataHash(string data)
  54. {
  55. try
  56. {
  57. using (var md5 = System.Security.Cryptography.MD5.Create())
  58. {
  59. // convert string to stream
  60. byte[] byteArray = Encoding.UTF8.GetBytes(data);
  61. using (MemoryStream stream = new MemoryStream(byteArray))
  62. {
  63. return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower();
  64. }
  65. }
  66. }
  67. catch (Exception)
  68. {
  69. return string.Empty;
  70. }
  71. }
  72. }
  73. public class SHA384
  74. {
  75. public static byte[] Hash(string key, string value)
  76. {
  77. byte[] keyBytes = Encoding.UTF8.GetBytes(key);
  78. byte[] data = Encoding.UTF8.GetBytes(value);
  79. byte[] result = new HMAC2(HashFactories.SHA384, keyBytes).ComputeHash(data);
  80. return result;
  81. }
  82. }
  83. public class SHA256
  84. {
  85. public static string Hash(string value)
  86. {
  87. byte[] valueBytes = Encoding.Unicode.GetBytes(value);
  88. HashAlgorithm hash = new SHA256CryptoServiceProvider();
  89. byte[] hashBytes = hash.ComputeHash(valueBytes);
  90. return Convert.ToBase64String(hashBytes);
  91. }
  92. public static string Hash(string value, string salt1, string salt2)
  93. {
  94. SHA256Managed hash = new SHA256Managed();
  95. SHA1 sha1 = new SHA1Managed();
  96. // gen salt2 hash
  97. byte[] dataSalt2 = Encoding.UTF8.GetBytes(salt2);
  98. byte[] salt2Bytes = hash.ComputeHash(dataSalt2);
  99. string salt2Str = string.Empty;
  100. foreach (byte x in salt2Bytes)
  101. {
  102. salt2Str += String.Format("{0:x2}", x);
  103. }
  104. string dataStr = salt1 + value + salt2Str;
  105. byte[] dataStrBytes = Encoding.UTF8.GetBytes(dataStr);
  106. byte[] shaBytes = sha1.ComputeHash(dataStrBytes);
  107. string sha1Str = string.Empty;
  108. foreach (byte x in shaBytes)
  109. {
  110. sha1Str += String.Format("{0:x2}", x);
  111. }
  112. byte[] sha1Bytes = Encoding.UTF8.GetBytes(sha1Str);
  113. byte[] valueBytes = hash.ComputeHash(sha1Bytes);
  114. string hashString = string.Empty;
  115. foreach (byte x in valueBytes)
  116. {
  117. hashString += String.Format("{0:x2}", x);
  118. }
  119. return hashString;
  120. }
  121. public static System.Security.Cryptography.SHA256 CreateHashAlgorithm()
  122. {
  123. if (CryptoConfig.AllowOnlyFipsAlgorithms)
  124. {
  125. return new SHA256CryptoServiceProvider();
  126. }
  127. return new SHA256Managed();
  128. }
  129. }
  130. public class AES
  131. {
  132. public static byte[] Decrypt(byte[] data, byte[] key, byte[] iv)
  133. {
  134. return Decrypt(data, key, iv, "CTR", "NoPadding");
  135. }
  136. public static byte[] Decrypt(byte[] data, string key, string iv)
  137. {
  138. byte[] keyBytes = Encoding.UTF8.GetBytes(key);
  139. byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
  140. return Decrypt(data, keyBytes, ivBytes, "CTR", "NoPadding");
  141. }
  142. public static byte[] Decrypt(byte[] data, byte[] key, byte[] iv, string mode, string padding)
  143. {
  144. using (MemoryStream stream = new MemoryStream(data))
  145. {
  146. return ProcessCipher(false, stream, 1024, key, iv, mode, padding);
  147. }
  148. }
  149. public static byte[] Encrypt(byte[] data, byte[] key, byte[] iv)
  150. {
  151. return Encrypt(data, key, iv, "CTR", "NoPadding");
  152. }
  153. public static byte[] Encrypt(byte[] data, string key, string iv)
  154. {
  155. byte[] keyBytes = Encoding.UTF8.GetBytes(key);
  156. byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
  157. return Encrypt(data, keyBytes, ivBytes, "CTR", "NoPadding");
  158. }
  159. public static byte[] Encrypt(byte[] data, byte[] key, byte[] iv, string mode, string padding)
  160. {
  161. using (MemoryStream stream = new MemoryStream(data))
  162. {
  163. return ProcessCipher(true, stream, 1024, key, iv, mode, padding);
  164. }
  165. }
  166. public static byte[] ProcessCipher(bool encrypt, Stream input, int blockSize, byte[] key, byte[] iv, string mode, string padding)
  167. {
  168. IBufferedCipher cipher = CipherUtilities.GetCipher("AES/" + mode + "/" + padding);
  169. cipher.Init(encrypt, new ParametersWithIV(ParameterUtilities.CreateKeyParameter("AES", key), iv));
  170. // Make sure the input stream is at the beginning
  171. input.Seek(0, SeekOrigin.Begin);
  172. // Initialize variables
  173. byte[] output = new byte[input.Length];
  174. int cipherOffset = 0;
  175. int processedBytes = 0;
  176. // Process the stream and save the bytes to the output
  177. do
  178. {
  179. processedBytes = ProcessCipherBlock(cipher, input, blockSize, output, cipherOffset);
  180. cipherOffset += processedBytes;
  181. }
  182. while (processedBytes > 0);
  183. // Finalize processing of the cipher
  184. cipher.DoFinal(output, cipherOffset);
  185. return output;
  186. }
  187. public static void EncryptToFile(string filePath, Stream input, int blockSize, byte[] key, byte[] iv, string mode, string padding)
  188. {
  189. IBufferedCipher cipher = CipherUtilities.GetCipher("AES/" + mode + "/" + padding);
  190. cipher.Init(true, new ParametersWithIV(ParameterUtilities.CreateKeyParameter("AES", key), iv));
  191. // Make sure the input stream is at the beginning
  192. input.Seek(0, SeekOrigin.Begin);
  193. using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
  194. {
  195. int processedBytes = 0;
  196. byte[] buffer = new byte[blockSize];
  197. do
  198. {
  199. processedBytes = ProcessCipherBlock(cipher, input, blockSize, buffer, 0);
  200. if (processedBytes > 0)
  201. {
  202. // We have bytes, lets write them to the file
  203. fileStream.Write(buffer, 0, processedBytes);
  204. // Clear the buffer
  205. Array.Clear(buffer, 0, blockSize);
  206. }
  207. }
  208. while (processedBytes > 0);
  209. // Clear the buffer
  210. Array.Clear(buffer, 0, blockSize);
  211. // Do the final output
  212. processedBytes = cipher.DoFinal(buffer, 0);
  213. if (processedBytes > 0)
  214. {
  215. // We have bytes, lets write them to the file
  216. fileStream.Write(buffer, 0, processedBytes);
  217. }
  218. }
  219. }
  220. public static int ProcessCipherBlock(IBufferedCipher cipher, Stream input, int blockSize, byte[] output, int outputOffset)
  221. {
  222. // Initialize buffer
  223. byte[] buffer = new byte[blockSize];
  224. // Read the next block of data
  225. int bytesRead = input.Read(buffer, 0, blockSize);
  226. if (bytesRead > 0)
  227. {
  228. // process the cipher for the read block and add it to the output
  229. return cipher.ProcessBytes(buffer, 0, bytesRead, output, outputOffset);
  230. }
  231. return 0;
  232. }
  233. public static byte[] CreateKey(string password, string iv, int keySize = 256)
  234. {
  235. byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
  236. return CreateKey(password, ivBytes, keySize);
  237. }
  238. public static byte[] CreateKey(string password, byte[] iv, int keySize = 256)
  239. {
  240. const int Iterations = 300;
  241. var keyGenerator = new Rfc2898DeriveBytes(password, iv, Iterations);
  242. return keyGenerator.GetBytes(keySize / 8);
  243. }
  244. }
  245. public static class PGP
  246. {
  247. public static bool IsPublicKey(string key)
  248. {
  249. bool isValid = false;
  250. try
  251. {
  252. byte[] byteArray = Encoding.ASCII.GetBytes(key);
  253. using (MemoryStream stream = new MemoryStream(byteArray))
  254. {
  255. using (Stream decoderStream = PgpUtilities.GetDecoderStream(stream))
  256. {
  257. PgpPublicKeyRingBundle publicKeyBundle = new PgpPublicKeyRingBundle(decoderStream);
  258. PgpPublicKey foundKey = GetFirstPublicKey(publicKeyBundle);
  259. if (foundKey != null)
  260. {
  261. isValid = true;
  262. }
  263. }
  264. }
  265. }
  266. catch (Exception ex)
  267. {
  268. isValid = false;
  269. }
  270. return isValid;
  271. }
  272. public static string GetFingerprint(string key)
  273. {
  274. string hexString = string.Empty;
  275. byte[] byteArray = Encoding.ASCII.GetBytes(key);
  276. using (MemoryStream stream = new MemoryStream(byteArray))
  277. {
  278. using (Stream decoderStream = PgpUtilities.GetDecoderStream(stream))
  279. {
  280. PgpPublicKeyRingBundle publicKeyBundle = new PgpPublicKeyRingBundle(decoderStream);
  281. PgpPublicKey foundKey = GetFirstPublicKey(publicKeyBundle);
  282. if (foundKey != null)
  283. {
  284. byte[] fing = foundKey.GetFingerprint();
  285. hexString = Hex.ToHexString(fing);
  286. }
  287. }
  288. }
  289. return hexString;
  290. }
  291. public static string GetFingerprint64(string key)
  292. {
  293. string fingerprint = GetFingerprint(key);
  294. if (fingerprint.Length > 16)
  295. fingerprint = fingerprint.Substring(fingerprint.Length - 16);
  296. return fingerprint;
  297. }
  298. private static PgpPublicKey GetFirstPublicKey(PgpPublicKeyRingBundle publicKeyRingBundle)
  299. {
  300. foreach (PgpPublicKeyRing kRing in publicKeyRingBundle.GetKeyRings())
  301. {
  302. var keys = kRing.GetPublicKeys();
  303. foreach (var key in keys)
  304. {
  305. PgpPublicKey foundKey = (PgpPublicKey)key;
  306. //PgpPublicKey key = kRing.GetPublicKeys()
  307. //.Cast<PgpPublicKey>()
  308. // .Where(k => k.IsEncryptionKey)
  309. // .FirstOrDefault();
  310. if (foundKey != null && foundKey.IsEncryptionKey)
  311. return foundKey;
  312. }
  313. }
  314. return null;
  315. }
  316. }
  317. }