-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncryptUtil.cs
175 lines (141 loc) · 6.28 KB
/
EncryptUtil.cs
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System;
namespace Utility
{
/// <summary>
/// Encryption utility functions.
/// </summary>
public sealed class EncryptUtil
{
private EncryptUtil() { } // Makes this a static class. No instantiation.
public readonly static string SharedCompiledKey = "SharedEncryptionPassword";
public readonly static string DefaultSaltValue = "3BB84119FC0743E194F2D20111FBD2C2";
public static readonly string EncryptionFlag = "986A2AE1CB3A4ED984876CF8B877B20A";
/// <summary>
/// Get an MD5 Hash for an input string
/// </summary>
static public string GetMd5Hash(string input)
{
if (string.IsNullOrEmpty(input)) return string.Empty;
byte[] data;
using (MD5 md5Hash = MD5.Create())
{
data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sb.Append(data[i].ToString("x2").ToLower());
}
return sb.ToString();
}
public static bool IsDataEncrypted(string data)
{
bool isEncrypted = false;
if (!string.IsNullOrEmpty(data) && data.Contains(EncryptUtil.EncryptionFlag, StringComparison.OrdinalIgnoreCase) && data.Substring(0, 32).EqualsIgnoreCase((EncryptUtil.EncryptionFlag)))
{
isEncrypted = true;
}
return isEncrypted;
}
public static string AddEncryptionFlag(string data)
{
//if(!string.IsNullOrEmpty(data) && data.Contains(EncryptUtil.EncryptionFlag))
StringBuilder finalData = new StringBuilder();
if (!string.IsNullOrEmpty(data))
{
if (EncryptUtil.IsDataEncrypted(data))
{
throw new Exception("Data is already encrypted. Cannot double encrypt the data");
}
finalData.Append(EncryptUtil.EncryptionFlag);
finalData.Append(data);
}
return finalData.ToString();
}
public static string RemoveEncryptionFlag(string data)
{
StringBuilder finalData = new StringBuilder();
if (!string.IsNullOrEmpty(data) && data.Contains(EncryptUtil.EncryptionFlag, StringComparison.OrdinalIgnoreCase) && data.Substring(0, 32).EqualsIgnoreCase((EncryptUtil.EncryptionFlag)))
{
finalData.Append(data.Remove(0, 32));
}
return finalData.ToString();
}
public static string Encrypt<T>(string symmetricKey, string textToEncrypt, string salt)
where T : SymmetricAlgorithm, new()
{
StringBuilder finalString = new StringBuilder();
if (string.IsNullOrEmpty(symmetricKey))
throw new Exception("symmetricKey cannot be null or empty.");
if (string.IsNullOrEmpty(salt))
throw new Exception("salt cannot be null or empty.");
if (string.IsNullOrEmpty(textToEncrypt))
return string.Empty;
textToEncrypt = textToEncrypt.Trim();
if (EncryptUtil.IsDataEncrypted(textToEncrypt))
{
finalString.Append(textToEncrypt);
}
else
{
DeriveBytes rgb = new Rfc2898DeriveBytes(symmetricKey, Encoding.Unicode.GetBytes(salt));
SymmetricAlgorithm algorithm = new T();
byte[] rgbKey = rgb.GetBytes(algorithm.KeySize >> 3);
byte[] rgbIV = rgb.GetBytes(algorithm.BlockSize >> 3);
ICryptoTransform transform = algorithm.CreateEncryptor(rgbKey, rgbIV);
using (MemoryStream buffer = new MemoryStream())
{
using (CryptoStream stream = new CryptoStream(buffer, transform, CryptoStreamMode.Write))
{
using (StreamWriter writer = new StreamWriter(stream, Encoding.Unicode))
{
writer.Write(textToEncrypt);
}
}
string encryptedString = Convert.ToBase64String(buffer.ToArray());
finalString.Append(EncryptUtil.AddEncryptionFlag(encryptedString));
}
}
return finalString.ToString();
}
public static string Decrypt<T>(string symmetricKey, string textToDecrypt, string salt)
where T : SymmetricAlgorithm, new()
{
if (string.IsNullOrEmpty(symmetricKey))
throw new Exception("symmetricKey cannot be null or empty.");
if (string.IsNullOrEmpty(salt))
throw new Exception("salt cannot be null or empty.");
if (string.IsNullOrEmpty(textToDecrypt))
return string.Empty;
string finalDecryptedString = string.Empty;
textToDecrypt = textToDecrypt.Trim();
if (EncryptUtil.IsDataEncrypted(textToDecrypt))
{
textToDecrypt = EncryptUtil.RemoveEncryptionFlag(textToDecrypt);
DeriveBytes rgb = new Rfc2898DeriveBytes(symmetricKey, Encoding.Unicode.GetBytes(salt));
SymmetricAlgorithm algorithm = new T();
byte[] rgbKey = rgb.GetBytes(algorithm.KeySize >> 3);
byte[] rgbIV = rgb.GetBytes(algorithm.BlockSize >> 3);
ICryptoTransform transform = algorithm.CreateDecryptor(rgbKey, rgbIV);
using (MemoryStream buffer = new MemoryStream(Convert.FromBase64String(textToDecrypt)))
{
using (CryptoStream stream = new CryptoStream(buffer, transform, CryptoStreamMode.Read))
{
using (StreamReader reader = new StreamReader(stream, Encoding.Unicode))
{
finalDecryptedString = reader.ReadToEnd();
}
}
}
}
else
{
finalDecryptedString = textToDecrypt;
}
return finalDecryptedString;
}
}
}