-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRijndaelEncryptor.cs
258 lines (224 loc) · 8.98 KB
/
RijndaelEncryptor.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
using EncryptedStorage.Service;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
namespace EncryptedStorage.Service
{
public class RijndaelEncryptor : IEncryptor
{
private readonly HttpContext context;
private readonly ILogger logger;
private readonly RijndaelManaged manager;
private readonly int[] sizes = { 16, 24, 32 };
public RijndaelEncryptor(IHttpContextAccessor context, ILoggerFactory loggerFactory)
{
this.context = context.HttpContext;
this.logger = loggerFactory.CreateLogger("RequestInfoLogger");
this.manager = new RijndaelManaged();
//manager.Padding = PaddingMode.PKCS7;
logger.LogInformation(manager.Padding.ToString());
}
public byte[] Encrypt(byte[] message)
{
manager.Key = ToByte(context.Session.GetString("StorageKey"));
manager.IV = ToByte(context.Session.GetString("StorageIV"));
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(ms, manager.CreateEncryptor(manager.Key, manager.IV), CryptoStreamMode.Write))
{
cryptoStream.Write(message, 0, message.Length);
}
return ms.ToArray();
}
}
public byte[] Encrypt(byte[] key, byte[] IV, byte[] message)
{
manager.Key = key;
manager.IV = IV;
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(ms, manager.CreateEncryptor(manager.Key, manager.IV), CryptoStreamMode.Write))
{
cryptoStream.Write(message, 0, message.Length);
}
return ms.ToArray();
}
}
public T Encrypt<T>(T obj)
{
manager.Key = ToByte(context.Session.GetString("StorageKey"));
manager.IV = ToByte(context.Session.GetString("StorageIV"));
Type type = obj.GetType();
MemoryStream memoryStream;
CryptoStream cryptoStream;
foreach (PropertyInfo p in type.GetProperties())
{
if (p.PropertyType != typeof(string))
continue;
if (p.GetValue(obj) == null)
continue;
memoryStream = new MemoryStream();
cryptoStream = new CryptoStream(memoryStream, manager.CreateEncryptor(manager.Key, manager.IV), CryptoStreamMode.Write);
byte[] property = ToByte(p.GetValue(obj).ToString());
cryptoStream.Write(property, 0, property.Length);
cryptoStream.FlushFinalBlock();
cryptoStream.Clear();
p.SetValue(obj, ToString(memoryStream.ToArray()));
memoryStream.Close();
}
return obj;
}
public byte[] Decrypt(byte[] message)
{
manager.Key = ToByte(context.Session.GetString("StorageKey"));
manager.IV = ToByte(context.Session.GetString("StorageIV"));
try
{
using (MemoryStream ms = new MemoryStream(message))
{
using (CryptoStream cryptoStream = new CryptoStream(ms, manager.CreateDecryptor(manager.Key, manager.IV), CryptoStreamMode.Read))
{
using (BinaryReader binaryReader = new BinaryReader(cryptoStream))
{
return binaryReader.ReadBytes(message.Length);
}
}
}
}
catch(Exception ex)
{
logger.LogInformation(ex.Message);
logger.LogInformation(ex.StackTrace);
return null;
}
}
public byte[] Decrypt(byte[] key, byte[] IV, byte[] message)
{
manager.Key = key;
manager.IV = IV;
try
{
using (MemoryStream ms = new MemoryStream(message))
{
using (CryptoStream cryptoStream = new CryptoStream(ms, manager.CreateDecryptor(manager.Key, manager.IV), CryptoStreamMode.Read))
{
using (BinaryReader binaryReader = new BinaryReader(cryptoStream))
{
return binaryReader.ReadBytes(message.Length);
}
}
}
}
catch (Exception ex)
{
logger.LogInformation(ex.Message);
logger.LogInformation(ex.StackTrace);
return null;
}
}
public T Decrypt<T>(T obj)
{
manager.Key = ToByte(context.Session.GetString("StorageKey"));
manager.IV = ToByte(context.Session.GetString("StorageIV"));
Type type = obj.GetType();
MemoryStream memoryStream;
CryptoStream cryptoStream;
StreamReader streamReader;
foreach (PropertyInfo p in type.GetProperties())
{
if (p.PropertyType != typeof(string))
continue;
byte[] property = ToByte(p.GetValue(obj).ToString());
memoryStream = new MemoryStream(property);
cryptoStream = new CryptoStream(memoryStream, manager.CreateDecryptor(manager.Key, manager.IV), CryptoStreamMode.Read);
streamReader = new StreamReader(cryptoStream);
p.SetValue(obj, streamReader.ReadToEnd());
streamReader.Close();
cryptoStream.Clear();
memoryStream.Close();
}
return obj;
}
public List<T> DecryptList<T>(List<T> objs)
{
manager.Key = ToByte(context.Session.GetString("StorageKey"));
manager.IV = ToByte(context.Session.GetString("StorageIV"));
MemoryStream memoryStream;
CryptoStream cryptoStream;
BinaryReader binaryReader;
for (int i = 0; i < objs.Count(); i++)
{
try
{
Type type = objs.ElementAt(i).GetType();
foreach (PropertyInfo p in type.GetProperties())
{
if (p.PropertyType != typeof(string))
continue;
byte[] property = ToByte(p.GetValue(objs.ElementAt(i)).ToString());
memoryStream = new MemoryStream(property);
cryptoStream = new CryptoStream(memoryStream, manager.CreateDecryptor(manager.Key, manager.IV), CryptoStreamMode.Read);
binaryReader = new BinaryReader(cryptoStream);
p.SetValue(objs.ElementAt(i), ToString(binaryReader.ReadBytes(property.Length)));
binaryReader.Close();
cryptoStream.Clear();
memoryStream.Close();
}
}
catch(Exception ex)
{
logger.LogInformation(ex.Message + " # " + ex.StackTrace);
continue;
}
}
return objs;
}
public async Task<bool> EncryptFile(IFormFile file, string path)
{
try
{
using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
{
var streamFile = file.OpenReadStream();
using (BinaryReader binaryReader = new BinaryReader(streamFile))
{
int length = (int)streamFile.Length;
var encrypted = Encrypt(binaryReader.ReadBytes(length));
await stream.WriteAsync(encrypted, 0, encrypted.Length);
}
}
return true;
}
catch (Exception ex) { return false; }
}
public bool CheckSizeKey(string key)
{
if (key == null) return false;
var length = key.Length;
var find = sizes.Where(s => s == length).FirstOrDefault();
if (find == 0)
return false;
return true;
}
public byte[] ToByte(string value)
{
return Encoding.GetEncoding(1251).GetBytes(value);
}
public string ToString(byte[] value)
{
return Encoding.GetEncoding(1251).GetString(value);
}
public byte[] GenerateIV()
{
manager.GenerateIV();
return manager.IV;
}
}
}