-
Notifications
You must be signed in to change notification settings - Fork 1
/
CryptoUtil.cs
226 lines (198 loc) · 6.11 KB
/
CryptoUtil.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace SocketSingleSend
{
static class CryptoUtil
{
//Because of AesCryptoServiceProvider.KeySize = 256
private const int DEFAULT_KEYSIZE = 256;
private static byte[] keyByte;
private static byte[] ivByte;
private static string key;
private static string iv;
public static string Key
{
get => key;
set
{
if (string.IsNullOrEmpty(value))
throw new ArgumentNullException("Key");
if (value.Length != (DEFAULT_KEYSIZE / 16))
throw new ArgumentException("Key");
key = value;
keyByte = StrToByte(key);
}
}
public static string IV
{
get => iv;
set
{
if (string.IsNullOrEmpty(value))
throw new ArgumentNullException("IV");
iv = value;
ivByte = StrToByte(iv);
}
}
static CryptoUtil()
{
using (AesCryptoServiceProvider aesCSP = new AesCryptoServiceProvider())
{
keyByte = aesCSP.Key;
ivByte = aesCSP.IV;
}
}
#region UtilMethod
public static byte[] StrToByte(string str)
{
return Encoding.UTF8.GetBytes(str);
}
public static string ByteToStr(byte[] strByte, int count = 0)
{
if (count == 0)
return Encoding.UTF8.GetString(strByte);
else
return Encoding.UTF8.GetString(strByte, 0, count);
}
public static string ByteToBase64(byte[] strByte)
{
return Convert.ToBase64String(strByte);
}
public static byte[] Base64ToByte(string base64Str)
{
return Convert.FromBase64String(base64Str);
}
public static string ByteToHexStr(byte[] strByte)
{
StringBuilder sb = new StringBuilder();
foreach (var item in strByte)
{
sb.Append(item.ToString("x2"));
}
return sb.ToString();
}
public static byte[] IntToByte(int num)
{
return BitConverter.GetBytes(num);
}
public static int ByteToInt(byte[] numByte)
{
return BitConverter.ToInt32(numByte, 0);
}
#endregion
public static void Init(string key, string iv)
{
Key = key;
IV = iv;
}
public static void Init(byte[] keyBuf, byte[] ivBuf)
{
if (keyBuf == null || ivBuf == null)
throw new ArgumentNullException("keyBuf / ivBuf");
if (keyBuf.Length != (DEFAULT_KEYSIZE / 8))
throw new ArgumentException("keyBuf");
key = (iv = null);
keyByte = keyBuf;
ivByte = ivBuf;
}
public static byte[] AESCrypto(byte[] inputByte, bool encrypt)
{
if (inputByte == null || inputByte.Length == 0)
throw new ArgumentNullException("inputByte");
using (AesCryptoServiceProvider aesCSP = new AesCryptoServiceProvider())
{
try
{
ICryptoTransform cryptor = encrypt ? aesCSP.CreateEncryptor(keyByte, ivByte)
: aesCSP.CreateDecryptor(keyByte, ivByte);
byte[] outputByte = cryptor.TransformFinalBlock(inputByte,
0, inputByte.Length);
aesCSP.Clear();
return outputByte;
}
#if DEBUG
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
#else
catch { }
#endif
}
return inputByte;
}
public static byte[] Encrypt(byte[] dataByte)
{
//AES Mode
return AESCrypto(dataByte, true);
}
public static byte[] Encrypt(byte[] dataByte, byte[] keyByte, byte[] ivByte)
{
Init(keyByte, ivByte);
return Encrypt(dataByte);
}
public static string Encrypt(string str)
{
//ToBase64String
return ByteToBase64(Encrypt(StrToByte(str)));
}
public static string Encrypt(string str, string key, string iv)
{
Init(key, iv);
return Encrypt(str);
}
public static byte[] Decrypt(byte[] dataByte)
{
//AES Mode
return AESCrypto(dataByte, false);
}
public static byte[] Decrypt(byte[] dataByte, byte[] keyByte, byte[] ivByte)
{
Init(keyByte, ivByte);
return Decrypt(dataByte);
}
public static string Decrypt(string enStr)
{
//FromBase64String
return ByteToStr(Decrypt(Base64ToByte(enStr)));
}
public static string Decrypt(string enStr, string key, string iv)
{
Init(key, iv);
return Decrypt(enStr);
}
#region HashMethod
public static byte[] SHA256Hash(byte[] strByte)
{
if (strByte == null || strByte.Length == 0)
throw new ArgumentNullException("strByte");
using (SHA256Managed sha = new SHA256Managed())
{
try
{
byte[] hashByte = sha.ComputeHash(strByte);
sha.Clear();
return hashByte;
}
#if DEBUG
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
#else
catch { }
#endif
}
return strByte;
}
public static string SHA256Hash(string str)
{
return ByteToHexStr(SHA256Hash(StrToByte(str)));
}
#endregion
}
}