Skip to content

Commit 27cb780

Browse files
committed
add new feature for aes encrypt/decrypt
1 parent d2fd0b3 commit 27cb780

File tree

1 file changed

+62
-5
lines changed

1 file changed

+62
-5
lines changed

src/NETCore.Encrypt/EncryptProvider.cs

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ private static string GetRandomStr(int length)
3030
StringBuilder num = new StringBuilder();
3131

3232
Random rnd = new Random(DateTime.Now.Millisecond);
33-
for (int i = 0;i < length;i++)
33+
for (int i = 0; i < length; i++)
3434
{
3535
num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString());
3636
}
@@ -74,6 +74,33 @@ public static string AESEncrypt(string data, string key, string vector)
7474
Check.Argument.IsNotOutOfRange(vector.Length, 16, 16, nameof(vector));
7575

7676
Byte[] plainBytes = Encoding.UTF8.GetBytes(data);
77+
78+
var encryptBytes = AESEncrypt(plainBytes, key, vector);
79+
if (encryptBytes == null)
80+
{
81+
return null;
82+
}
83+
return Convert.ToBase64String(encryptBytes);
84+
}
85+
86+
/// <summary>
87+
/// AES encrypt
88+
/// </summary>
89+
/// <param name="data">Raw data</param>
90+
/// <param name="key">Key, requires 32 bits</param>
91+
/// <param name="vector">IV,requires 16 bits</param>
92+
/// <returns>Encrypted byte array</returns>
93+
public static byte[] AESEncrypt(byte[] data, string key, string vector)
94+
{
95+
Check.Argument.IsNotEmpty(data, nameof(data));
96+
97+
Check.Argument.IsNotEmpty(key, nameof(key));
98+
Check.Argument.IsNotOutOfRange(key.Length, 32, 32, nameof(key));
99+
100+
Check.Argument.IsNotEmpty(vector, nameof(vector));
101+
Check.Argument.IsNotOutOfRange(vector.Length, 16, 16, nameof(vector));
102+
103+
Byte[] plainBytes = data;
77104
Byte[] bKey = new Byte[32];
78105
Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length);
79106
Byte[] bVector = new Byte[16];
@@ -101,7 +128,7 @@ public static string AESEncrypt(string data, string key, string vector)
101128
{
102129
encryptData = null;
103130
}
104-
return Convert.ToBase64String(encryptData);
131+
return encryptData;
105132
}
106133
}
107134

@@ -123,6 +150,35 @@ public static string AESDecrypt(string data, string key, string vector)
123150
Check.Argument.IsNotOutOfRange(vector.Length, 16, 16, nameof(vector));
124151

125152
Byte[] encryptedBytes = Convert.FromBase64String(data);
153+
154+
Byte[] decryptBytes = AESDecrypt(encryptedBytes, key, vector);
155+
156+
if (decryptBytes == null)
157+
{
158+
return null;
159+
}
160+
return Encoding.UTF8.GetString(decryptBytes);
161+
}
162+
163+
/// <summary>
164+
/// AES decrypt
165+
/// </summary>
166+
/// <param name="data">Encrypted data</param>
167+
/// <param name="key">Key, requires 32 bits</param>
168+
/// <param name="vector">IV,requires 16 bits</param>
169+
/// <returns>Decrypted byte array</returns>
170+
171+
public static byte[] AESDecrypt(byte[] data, string key, string vector)
172+
{
173+
Check.Argument.IsNotEmpty(data, nameof(data));
174+
175+
Check.Argument.IsNotEmpty(key, nameof(key));
176+
Check.Argument.IsNotOutOfRange(key.Length, 32, 32, nameof(key));
177+
178+
Check.Argument.IsNotEmpty(vector, nameof(vector));
179+
Check.Argument.IsNotOutOfRange(vector.Length, 16, 16, nameof(vector));
180+
181+
Byte[] encryptedBytes = data;
126182
Byte[] bKey = new Byte[32];
127183
Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length);
128184
Byte[] bVector = new Byte[16];
@@ -156,7 +212,8 @@ public static string AESDecrypt(string data, string key, string vector)
156212
{
157213
decryptedData = null;
158214
}
159-
return Encoding.UTF8.GetString(decryptedData);
215+
216+
return decryptedData;
160217
}
161218
}
162219

@@ -437,7 +494,7 @@ public static RSAKey CreateRsaKey(RsaSize rsaSize = RsaSize.R2048)
437494
{
438495
using (RSA rsa = RSA.Create())
439496
{
440-
rsa.KeySize = (int) rsaSize;
497+
rsa.KeySize = (int)rsaSize;
441498

442499
string publicKey = rsa.ToJsonString(false);
443500
string privateKey = rsa.ToJsonString(true);
@@ -761,7 +818,7 @@ private static string CreateMachineKey(int length)
761818
rng.GetBytes(random);
762819

763820
StringBuilder machineKey = new StringBuilder(length);
764-
for (int i = 0;i < random.Length;i++)
821+
for (int i = 0; i < random.Length; i++)
765822
{
766823
machineKey.Append(string.Format("{0:X2}", random[i]));
767824
}

0 commit comments

Comments
 (0)