| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- using System;
- using System.IO;
- using System.Security.Cryptography;
- using System.Text;
- namespace Quadarax.Foundation.Common.Security
- {
- public static class Secure
- {
- static string _passwordHash = "OmMC:7i%+o.MlIeg`kRcv:R3haPyx|#vuM,G,oB1n=IBUG'!M6";
- static string _saltKey = "vo:Y:pWYcS\"*m@^6R\\O#$eSg!,23H\\R8KfIi\\u1IED91TE0J#~";
- static string _vIKey = "@!PYn,b=t4\\%`Am`z!ubwE1H2Ek1?*R&,6cF:f1mZJ1dxB&DZH";
- public static void SetProperties(string passwordHash, string saltKey, string vIKey)
- {
- _passwordHash = passwordHash;
- _saltKey = saltKey;
- _vIKey = vIKey;
- }
- public static string Encrypt(string plainText)
- {
- var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
- var keyBytes = new Rfc2898DeriveBytes(_passwordHash, Encoding.ASCII.GetBytes(_saltKey)).GetBytes(256 / 8);
- var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.Zeros };
- var encryptor = symmetricKey.CreateEncryptor(keyBytes, Encoding.ASCII.GetBytes(_vIKey));
-
- byte[] cipherTextBytes;
- using (var memoryStream = new MemoryStream())
- {
- using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
- {
- cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
- cryptoStream.FlushFinalBlock();
- cipherTextBytes = memoryStream.ToArray();
- cryptoStream.Close();
- }
- memoryStream.Close();
- }
- return Convert.ToBase64String(cipherTextBytes);
- }
- public static string Decrypt(string encryptedText)
- {
- var cipherTextBytes = Convert.FromBase64String(encryptedText);
- var keyBytes = new Rfc2898DeriveBytes(_passwordHash, Encoding.ASCII.GetBytes(_saltKey)).GetBytes(256 / 8);
- var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.None };
- var decryptor = symmetricKey.CreateDecryptor(keyBytes, Encoding.ASCII.GetBytes(_vIKey));
- var memoryStream = new MemoryStream(cipherTextBytes);
- var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
- byte[] plainTextBytes = new byte[cipherTextBytes.Length];
- int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
- memoryStream.Close();
- cryptoStream.Close();
- return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount).TrimEnd("\0".ToCharArray());
- }
- }
- }
|