-
Notifications
You must be signed in to change notification settings - Fork 1
/
Endec.cs
236 lines (207 loc) · 7.72 KB
/
Endec.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
//********************************************************************************************
// Author: Sergiy Stoyan
// s.y.stoyan@gmail.com, sergiy.stoyan@outlook.com, stoyan@cliversoft.com
// http://www.cliversoft.com
//********************************************************************************************
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;
using System.Security.Cryptography;
namespace Cliver
{
public abstract class Endec
{
abstract public byte[] Encrypt(byte[] bytes);
abstract public byte[] Decrypt(byte[] bytes);
public class Rijndael : Endec
{
//readonly string key;
readonly byte[] vector = new byte[] { 0x26, 0xdc, 0xff, 0x00, 0xad, 0xed, 0x7a, 0xee, 0xc5, 0xfe, 0x07, 0xaf, 0x4d, 0x08, 0x22, 0x3c };
public Rijndael(string key)
{
if (key == null)
throw new ArgumentNullException("key");
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(key, vector);
// rijndael.Padding = PaddingMode.Zeros;
rijndael.Key = pdb.GetBytes(32);
rijndael.IV = pdb.GetBytes(16);
}
System.Security.Cryptography.Rijndael rijndael = System.Security.Cryptography.Rijndael.Create();
override public byte[] Encrypt(byte[] bytes)
{
using (MemoryStream ms = new MemoryStream())
using (CryptoStream cs = new CryptoStream(ms, rijndael.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(bytes, 0, bytes.Length);
//cs.FlushFinalBlock();
cs.Close();
return ms.ToArray();
}
}
override public byte[] Decrypt(byte[] bytes)
{
using (MemoryStream ms = new MemoryStream())
using (CryptoStream cs = new CryptoStream(ms, rijndael.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(bytes, 0, bytes.Length);
//cs.FlushFinalBlock();
cs.Close();
return ms.ToArray();
}
}
}
public class Aes : Endec
{
readonly string key = null;
public Aes(string key)
{
this.key = key ?? throw new ArgumentNullException("key");
}
public override byte[] Encrypt(byte[] bytes)
{
using (System.Security.Cryptography.Aes encryptor = System.Security.Cryptography.Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(key, 8);
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(bytes, 0, bytes.Length);
cs.Close();
}
return ms.ToArray();
}
}
}
public override byte[] Decrypt(byte[] bytes)
{
using (System.Security.Cryptography.Aes encryptor = System.Security.Cryptography.Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(key, 8);
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(bytes, 0, bytes.Length);
cs.Close();
}
return ms.ToArray();
}
}
}
}
}
public class StringEndec
{
public StringEndec(Endec endec)
{
this.endec = endec;
}
protected readonly Endec endec;
virtual public string Encrypt<T>(T o) where T : class
{
if (o == null)
return null;
byte[] bytes = o as byte[];
if (bytes == null)
{
string s = o as string;
if (s == null)
s = Serialization.Json.Serialize(o, false, true);
bytes = Encoding.Unicode.GetBytes(s);
}
return Convert.ToBase64String(endec.Encrypt(bytes));
}
virtual public T Decrypt<T>(string s) where T : class
{
if (s == null)
return null;
s = s.Replace(" ", "+");
byte[] bytes = Convert.FromBase64String(s);
bytes = endec.Decrypt(bytes);
if (typeof(T) == typeof(byte[]))
return bytes as T;
s = Encoding.Unicode.GetString(bytes);
if (typeof(T) == typeof(string))
return s as T;
return Serialization.Json.Deserialize<T>(s, true);
}
public class Rijndael : StringEndec
{
public Rijndael(string key) : base(new Endec.Rijndael(key))
{
}
}
}
//public class StringEndec<T> : StringEndec where T : class
//{
// public StringEndec(Endec endec) : base(endec) { }
// virtual public string Encrypt(T o)
// {
// return Encrypt<T>(o);
// }
// virtual public T Decrypt(string s)
// {
// return Decrypt<T>(s);
// }
// public class Rijndael : StringEndec<T>
// {
// public Rijndael(string key) : base(new Endec.Rijndael(key))
// {
// }
// }
//}
//public class EndecBytes2String : StringEndec
//{
// public EndecBytes2String(Endec endec) : base(endec)
// { }
// public string Encrypt(byte[] bytes)
// {
// return Convert.ToBase64String(endec.Encrypt(bytes));
// }
// public byte[] Decrypt(string s)
// {
// s = s.Replace(" ", "+");
// return endec.Decrypt(Convert.FromBase64String(s));
// }
//}
//public class EndecString2String : StringEndec
//{
// public EndecString2String(Endec endec) : base(endec)
// { }
// public string Encrypt(string s)
// {
// byte[] bytes = Encoding.Unicode.GetBytes(s);
// return Convert.ToBase64String(endec.Encrypt(bytes));
// }
// public string Decrypt(string s)
// {
// s = s.Replace(" ", "+");
// byte[] bytes = endec.Decrypt(Convert.FromBase64String(s));
// return Encoding.Unicode.GetString(bytes);
// }
//}
//public class EndecObject2String<T>: StringEndec where T : class
//{
// public EndecObject2String(Endec endec) : base(endec)
// { }
// public string Encrypt(T o)
// {
// string s = Serialization.Json.Serialize(o, false, true);
// byte[] bytes = Encoding.Unicode.GetBytes(s);
// return Convert.ToBase64String(endec.Encrypt(bytes));
// }
// public T Decrypt(string s)
// {
// s = s.Replace(" ", "+");
// byte[] bytes = endec.Decrypt(Convert.FromBase64String(s));
// s = Encoding.Unicode.GetString(bytes);
// return Serialization.Json.Deserialize<T>(s);
// }
//}
}