Secure.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. namespace Quadarax.Foundation.Common.Security
  6. {
  7. public static class Secure
  8. {
  9. /*
  10. static string _passwordHash = "/:-s*oQ9VG~!]}))ud?ar*(5RanaWm{LHO-7Ncr[lvXGhB0=./raTYVGhO)n$ioE:tH1-TV.h57J+V-K+nVONHvAGeFa+0;R,`LUzqG&KI%-JQO6x>_/L'[sP2Cuz{z?9ch<2F~Cw@}@LNMpmv'viP+YHb}Z%.#dT*`5:nabn5n;8g1Tm<@,Zk(F9m{[s17*lc*P^}}HD,Mh]wnwaZfvYA`uCQ)Or&~cLy6wlYd_fw?ljr5i]l!UFhC'[sTbOaru";
  11. static string _saltKey = "Unx.Y16ju9K)!@uA>9#DVxan%Wr,&c~[2q?T}Q'~XBweCgtvL7+&zv%(9uQxn#gnDYE_(IgQ%sUK-!n+{d-{vBhLXXbUJ$G]SQiXt.#S(WS=oQPTYU|%{o)fN=90/KgYfo&5$&hL}JAu8.+2]kPoRM1Rr&=prDKBpahWjjr?q;.`_8hrjJ]Y-ap88N$FcR,%SkyV)(ssdg1~A&3i?|n`yoJPBMy$];Q=%3b/^}|6<Dj!Agna3K;27gCl2Y9*vhqH";
  12. static string _vIKey = "29T|l'6$&Z}(NpQ8]~d1U-kVO@ot8)&w$uf&UsDBKfcB_1rQ0'5H+w.EHKlS5)14AiBa(Kg4dmOpfan0s%eGwxy$1:|lRrkBLZJndV/l3;`W(Plx[n88TD]'kk@wYl_dA7#DP2|]V03~mkRh!8{$&bUTF585Zjb@B}d'2jo7n:7VA-e|X!nf7e$xP`dBh`DAE=Q~_TOVH;gJDK}Q>f@q.u&#2yOK=!F,$/{F5BxGvqUt$?ry0AmKNOH:y}XJq_x_";
  13. */
  14. private static string _passwordHash = "Vk&G#lG}vv@5S55x";
  15. private static string _saltKey = "vzb4[}[L%<='(U!n";
  16. private static string _vIKey = "&C%:gIt<P-|%nA45";
  17. public static void SetProperties(string passwordHash, string saltKey, string vIKey)
  18. {
  19. if (passwordHash.Length != 16)
  20. throw new ArgumentOutOfRangeException(nameof(passwordHash),"Value must have 16 chars.");
  21. if (saltKey.Length != 16)
  22. throw new ArgumentOutOfRangeException(nameof(saltKey),"Value must have 16 chars.");
  23. if (vIKey.Length != 16)
  24. throw new ArgumentOutOfRangeException(nameof(vIKey),"Value must have 16 chars.");
  25. _passwordHash = passwordHash;
  26. _saltKey = saltKey;
  27. _vIKey = vIKey;
  28. }
  29. public static string Encrypt(string plainText)
  30. {
  31. if (string.IsNullOrEmpty(plainText))
  32. return string.Empty;
  33. var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
  34. var keyBytes = new Rfc2898DeriveBytes(_passwordHash, Encoding.UTF8.GetBytes(_saltKey)).GetBytes(256/8);
  35. var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.Zeros};
  36. var encryptor = symmetricKey.CreateEncryptor(keyBytes, Encoding.ASCII.GetBytes(_vIKey));
  37. byte[] cipherTextBytes;
  38. using (var memoryStream = new MemoryStream())
  39. {
  40. using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
  41. {
  42. cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
  43. cryptoStream.FlushFinalBlock();
  44. cipherTextBytes = memoryStream.ToArray();
  45. cryptoStream.Close();
  46. }
  47. memoryStream.Close();
  48. }
  49. return Convert.ToBase64String(cipherTextBytes);
  50. }
  51. public static string Decrypt(string encryptedText)
  52. {
  53. if (string.IsNullOrEmpty(encryptedText))
  54. return string.Empty;
  55. var cipherTextBytes = Convert.FromBase64String(encryptedText);
  56. var keyBytes = new Rfc2898DeriveBytes(_passwordHash, Encoding.ASCII.GetBytes(_saltKey)).GetBytes(256 / 8);
  57. var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.None };
  58. var decryptor = symmetricKey.CreateDecryptor(keyBytes, Encoding.ASCII.GetBytes(_vIKey));
  59. var memoryStream = new MemoryStream(cipherTextBytes);
  60. var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
  61. byte[] plainTextBytes = new byte[cipherTextBytes.Length];
  62. int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
  63. memoryStream.Close();
  64. cryptoStream.Close();
  65. return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount).TrimEnd("\0".ToCharArray());
  66. }
  67. }
  68. }