using Gestor.Common.Helpers; using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace Gestor.Common.Security { public class Token { private readonly byte[] _key = new byte[] { 45, 103, 73, 146, 210, 184, 220, 224, 94, 3, 114, 60, 211, 119, 21, 100, 18, 201, 230, 195, 119, 252, 73, 208, 209, 39, 222, 48, 47, 142, 94, 24 }; private readonly byte[] _initializationVector = new byte[] { 95, 17, 151, 243, 209, 243, 119, 80, 63, 252, 13, 180, 162, 13, 23, 218 }; private readonly static byte[] Salt; static Token() { Token.Salt = new byte[] { 38, 220, 255, 0, 173, 237, 122, 238, 197, 254, 7, 175, 77, 8, 34, 60 }; } public Token() { } public string AggerDecrypt(string cipher) { string str = string.Concat("aGG3r", Convert.ToString(1012), "#w3BDz$"); byte[] numArray = Convert.FromBase64String(cipher); Rijndael bytes = Rijndael.Create(); Rfc2898DeriveBytes rfc2898DeriveByte = new Rfc2898DeriveBytes(str, Token.Salt); bytes.Key = rfc2898DeriveByte.GetBytes(32); bytes.IV = rfc2898DeriveByte.GetBytes(16); MemoryStream memoryStream = new MemoryStream(); CryptoStream cryptoStream = new CryptoStream(memoryStream, bytes.CreateDecryptor(), CryptoStreamMode.Write); cryptoStream.Write(numArray, 0, (int)numArray.Length); cryptoStream.Close(); byte[] array = memoryStream.ToArray(); return Encoding.UTF8.GetString(array); } public string AggerEncrypt(string plain) { string str = string.Concat("aGG3r", Convert.ToString(1012), "#w3BDz$"); byte[] bytes = Encoding.UTF8.GetBytes(plain); Rijndael rijndael = Rijndael.Create(); Rfc2898DeriveBytes rfc2898DeriveByte = new Rfc2898DeriveBytes(str, Token.Salt); rijndael.Key = rfc2898DeriveByte.GetBytes(32); rijndael.IV = rfc2898DeriveByte.GetBytes(16); MemoryStream memoryStream = new MemoryStream(); CryptoStream cryptoStream = new CryptoStream(memoryStream, rijndael.CreateEncryptor(), CryptoStreamMode.Write); cryptoStream.Write(bytes, 0, (int)bytes.Length); cryptoStream.Close(); return Convert.ToBase64String(memoryStream.ToArray()); } public string Decrypt(string plainText) { string end; string str; try { byte[] numArray = Convert.FromBase64String(plainText); using (AesCryptoServiceProvider aesCryptoServiceProvider = new AesCryptoServiceProvider()) { ICryptoTransform cryptoTransform = aesCryptoServiceProvider.CreateDecryptor(this._key, this._initializationVector); using (MemoryStream memoryStream = new MemoryStream(numArray)) { using (CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Read)) { using (StreamReader streamReader = new StreamReader(cryptoStream)) { end = streamReader.ReadToEnd(); } } } } str = end; } catch (Exception exception) { str = null; } return str; } public bool DefaultDecryption(string ecryptedText) { return this.DefaultVerification(this.Decrypt(ecryptedText).Split(new char[] { ':' })); } public string DefaultEncryption(long clientId, long providerId) { object obj = providerId; DateTime universalTime = Functions.GetNetworkTime().ToUniversalTime(); string str = string.Format("{0}:{1}", obj, universalTime.Ticks); return this.Encrypt(str); } public bool DefaultVerification(string[] textDecrypted) { if ((int)textDecrypted.Length < 2) { return false; } DateTime dateTime = new DateTime(long.Parse(textDecrypted[1]), DateTimeKind.Utc); dateTime = dateTime.Date; DateTime dateTime1 = dateTime.AddHours(5); dateTime = Functions.GetNetworkTime().ToUniversalTime(); return dateTime1 >= dateTime.Date; } public string Encrypt(string plainText) { byte[] array; using (AesCryptoServiceProvider aesCryptoServiceProvider = new AesCryptoServiceProvider()) { ICryptoTransform cryptoTransform = aesCryptoServiceProvider.CreateEncryptor(this._key, this._initializationVector); using (MemoryStream memoryStream = new MemoryStream()) { using (CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Write)) { using (StreamWriter streamWriter = new StreamWriter(cryptoStream)) { streamWriter.Write(plainText); } array = memoryStream.ToArray(); } } } return Convert.ToBase64String(array); } public bool VerifyToken(string authHeader) { bool flag; try { flag = (authHeader.Contains("Token") ? this.DefaultDecryption(authHeader.Replace("Token ", "")) : false); } catch (Exception exception) { flag = false; } return flag; } } }