summaryrefslogtreecommitdiff
path: root/Codemerx/Gestor.Common/Gestor.Common.Security/Token.cs
blob: c2d76b14903a357c045d1c10369e908339b1b082 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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;
		}
	}
}