|
|
@@ -4,75 +4,72 @@ using System.Text;
|
|
|
|
|
|
namespace Essensoft.AspNetCore.Payment.Security
|
|
|
{
|
|
|
- public enum AESCipherMode
|
|
|
- {
|
|
|
- CBC = CipherMode.CBC,
|
|
|
- ECB = CipherMode.ECB
|
|
|
- }
|
|
|
-
|
|
|
- public enum AESPaddingMode
|
|
|
- {
|
|
|
- PKCS7 = PaddingMode.PKCS7
|
|
|
- }
|
|
|
-
|
|
|
public class AES
|
|
|
{
|
|
|
- public static string Encrypt(string data, string key, byte[] iv, AESCipherMode cipherMode, AESPaddingMode paddingMode)
|
|
|
+ public static string Encrypt(string data, string key, byte[] iv, CipherMode cipherMode, PaddingMode paddingMode)
|
|
|
{
|
|
|
- var rm = new RijndaelManaged
|
|
|
+ using (var aes = Aes.Create())
|
|
|
{
|
|
|
- IV = iv,
|
|
|
- Key = Encoding.UTF8.GetBytes(key),
|
|
|
- Mode = (CipherMode)cipherMode,
|
|
|
- Padding = (PaddingMode)paddingMode
|
|
|
- };
|
|
|
+ aes.Key = Encoding.UTF8.GetBytes(key);
|
|
|
+ aes.IV = iv;
|
|
|
+ aes.Mode = cipherMode;
|
|
|
+ aes.Padding = paddingMode;
|
|
|
|
|
|
- var content = Encoding.UTF8.GetBytes(data);
|
|
|
- var ctf = rm.CreateEncryptor();
|
|
|
- return Convert.ToBase64String(ctf.TransformFinalBlock(content, 0, content.Length));
|
|
|
+ using (var ctf = aes.CreateEncryptor())
|
|
|
+ {
|
|
|
+ var content = Encoding.UTF8.GetBytes(data);
|
|
|
+ return Convert.ToBase64String(ctf.TransformFinalBlock(content, 0, content.Length));
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- public static string Decrypt(string data, string key, byte[] iv, AESCipherMode cipherMode, AESPaddingMode paddingMode)
|
|
|
+ public static string Decrypt(string data, string key, byte[] iv, CipherMode cipherMode, PaddingMode paddingMode)
|
|
|
{
|
|
|
- var rm = new RijndaelManaged
|
|
|
+ using (var aes = Aes.Create())
|
|
|
{
|
|
|
- IV = iv,
|
|
|
- Key = Encoding.UTF8.GetBytes(key),
|
|
|
- Mode = (CipherMode)cipherMode,
|
|
|
- Padding = (PaddingMode)paddingMode
|
|
|
- };
|
|
|
+ aes.Key = Encoding.UTF8.GetBytes(key);
|
|
|
+ aes.IV = iv;
|
|
|
+ aes.Mode = cipherMode;
|
|
|
+ aes.Padding = paddingMode;
|
|
|
|
|
|
- var content = Convert.FromBase64String(data);
|
|
|
- var ctf = rm.CreateDecryptor();
|
|
|
- return Encoding.UTF8.GetString(ctf.TransformFinalBlock(content, 0, content.Length));
|
|
|
+ using (var ctf = aes.CreateDecryptor())
|
|
|
+ {
|
|
|
+ var content = Convert.FromBase64String(data);
|
|
|
+ return Encoding.UTF8.GetString(ctf.TransformFinalBlock(content, 0, content.Length));
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- public static string Encrypt(string data, string key, AESCipherMode cipherMode, AESPaddingMode paddingMode)
|
|
|
+ public static string Encrypt(string data, string key, CipherMode cipherMode, PaddingMode paddingMode)
|
|
|
{
|
|
|
- var rm = new RijndaelManaged
|
|
|
+ using (var aes = Aes.Create())
|
|
|
{
|
|
|
- Key = Encoding.UTF8.GetBytes(key),
|
|
|
- Mode = (CipherMode)cipherMode,
|
|
|
- Padding = (PaddingMode)paddingMode
|
|
|
- };
|
|
|
+ aes.Key = Encoding.UTF8.GetBytes(key);
|
|
|
+ aes.Mode = cipherMode;
|
|
|
+ aes.Padding = paddingMode;
|
|
|
|
|
|
- var content = Encoding.UTF8.GetBytes(data);
|
|
|
- var ctf = rm.CreateEncryptor();
|
|
|
- return Convert.ToBase64String(ctf.TransformFinalBlock(content, 0, content.Length));
|
|
|
+ using (var ctf = aes.CreateEncryptor())
|
|
|
+ {
|
|
|
+ var content = Encoding.UTF8.GetBytes(data);
|
|
|
+ return Convert.ToBase64String(ctf.TransformFinalBlock(content, 0, content.Length));
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- public static string Decrypt(string data, string key, AESCipherMode cipherMode, AESPaddingMode paddingMode)
|
|
|
+ public static string Decrypt(string data, string key, CipherMode cipherMode, PaddingMode paddingMode)
|
|
|
{
|
|
|
- var rm = new RijndaelManaged
|
|
|
+ using (var aes = Aes.Create())
|
|
|
{
|
|
|
- Key = Encoding.UTF8.GetBytes(key),
|
|
|
- Mode = (CipherMode)cipherMode,
|
|
|
- Padding = (PaddingMode)paddingMode
|
|
|
- };
|
|
|
+ aes.Key = Encoding.UTF8.GetBytes(key);
|
|
|
+ aes.Mode = cipherMode;
|
|
|
+ aes.Padding = paddingMode;
|
|
|
|
|
|
- var content = Convert.FromBase64String(data);
|
|
|
- var ctf = rm.CreateDecryptor();
|
|
|
- return Encoding.UTF8.GetString(ctf.TransformFinalBlock(content, 0, content.Length));
|
|
|
+ using (var ctf = aes.CreateDecryptor())
|
|
|
+ {
|
|
|
+ var content = Convert.FromBase64String(data);
|
|
|
+ return Encoding.UTF8.GetString(ctf.TransformFinalBlock(content, 0, content.Length));
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|