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
|
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using Gestor.Common.Helpers;
namespace Gestor.Common.Security;
public class Token
{
private readonly byte[] _key = new byte[32]
{
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[16]
{
95, 17, 151, 243, 209, 243, 119, 80, 63, 252,
13, 180, 162, 13, 23, 218
};
private static readonly byte[] Salt = new byte[16]
{
38, 220, 255, 0, 173, 237, 122, 238, 197, 254,
7, 175, 77, 8, 34, 60
};
public bool VerifyToken(string authHeader)
{
try
{
if (!authHeader.Contains("Token"))
{
return false;
}
string ecryptedText = authHeader.Replace("Token ", "");
return DefaultDecryption(ecryptedText);
}
catch (Exception)
{
return false;
}
}
public string DefaultEncryption(long clientId, long providerId)
{
string plainText = $"{providerId}:{Functions.GetNetworkTime().ToUniversalTime().Ticks}";
return Encrypt(plainText);
}
public bool DefaultDecryption(string ecryptedText)
{
string[] textDecrypted = Decrypt(ecryptedText).Split(new char[1] { ':' });
return DefaultVerification(textDecrypted);
}
public bool DefaultVerification(string[] textDecrypted)
{
if (textDecrypted.Length < 2)
{
return false;
}
return new DateTime(long.Parse(textDecrypted[1]), DateTimeKind.Utc).Date.AddHours(5.0) >= Functions.GetNetworkTime().ToUniversalTime().Date;
}
public string Encrypt(string plainText)
{
byte[] inArray;
using (AesCryptoServiceProvider aesCryptoServiceProvider = new AesCryptoServiceProvider())
{
ICryptoTransform transform = aesCryptoServiceProvider.CreateEncryptor(_key, _initializationVector);
using MemoryStream memoryStream = new MemoryStream();
using CryptoStream stream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write);
using (StreamWriter streamWriter = new StreamWriter(stream))
{
streamWriter.Write(plainText);
}
inArray = memoryStream.ToArray();
}
return Convert.ToBase64String(inArray);
}
public string Decrypt(string plainText)
{
try
{
byte[] buffer = Convert.FromBase64String(plainText);
string result;
using (AesCryptoServiceProvider aesCryptoServiceProvider = new AesCryptoServiceProvider())
{
ICryptoTransform transform = aesCryptoServiceProvider.CreateDecryptor(_key, _initializationVector);
using MemoryStream stream = new MemoryStream(buffer);
using CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Read);
using StreamReader streamReader = new StreamReader(stream2);
result = streamReader.ReadToEnd();
}
return result;
}
catch (Exception)
{
return null;
}
}
public string AggerEncrypt(string plain)
{
string password = "aGG3r" + Convert.ToString(1012) + "#w3BDz$";
byte[] bytes = Encoding.UTF8.GetBytes(plain);
Rijndael rijndael = Rijndael.Create();
Rfc2898DeriveBytes rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, Salt);
rijndael.Key = rfc2898DeriveBytes.GetBytes(32);
rijndael.IV = rfc2898DeriveBytes.GetBytes(16);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, rijndael.CreateEncryptor(), CryptoStreamMode.Write);
cryptoStream.Write(bytes, 0, bytes.Length);
cryptoStream.Close();
return Convert.ToBase64String(memoryStream.ToArray());
}
public string AggerDecrypt(string cipher)
{
string password = "aGG3r" + Convert.ToString(1012) + "#w3BDz$";
byte[] array = Convert.FromBase64String(cipher);
Rijndael rijndael = Rijndael.Create();
Rfc2898DeriveBytes rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, Salt);
rijndael.Key = rfc2898DeriveBytes.GetBytes(32);
rijndael.IV = rfc2898DeriveBytes.GetBytes(16);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, rijndael.CreateDecryptor(), CryptoStreamMode.Write);
cryptoStream.Write(array, 0, array.Length);
cryptoStream.Close();
byte[] bytes = memoryStream.ToArray();
return Encoding.UTF8.GetString(bytes);
}
}
|