From 516f0fa1b44a6e1c9eb5a1891d21ab43c31acdbf Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Tue, 4 Jul 2023 15:24:06 +0800 Subject: [PATCH] =?UTF-8?q?hang=5FS=5F1=5F=E5=8F=AF=E9=80=86=E5=8A=A0?= =?UTF-8?q?=E5=AF=86=E7=AE=97=E6=B3=95=E5=A2=9E=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../_IRaCIS/SymmetricEncryption.cs | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 IRaCIS.Core.Infrastructure/_IRaCIS/SymmetricEncryption.cs diff --git a/IRaCIS.Core.Infrastructure/_IRaCIS/SymmetricEncryption.cs b/IRaCIS.Core.Infrastructure/_IRaCIS/SymmetricEncryption.cs new file mode 100644 index 000000000..f19c5dd73 --- /dev/null +++ b/IRaCIS.Core.Infrastructure/_IRaCIS/SymmetricEncryption.cs @@ -0,0 +1,46 @@ +using System; +using System.Security.Cryptography; +using System.Text; + +namespace IRaCIS.Core.Infrastructure +{ + public class SymmetricEncryption + { + public static string Encrypt(string plainText, string key) + { + byte[] keyBytes = Encoding.UTF8.GetBytes(key); + byte[] ivBytes = new byte[16]; // Initialization vector (IV) + + using (Aes aes = Aes.Create()) + { + aes.Key = keyBytes; + aes.IV = ivBytes; + ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV); + + byte[] plainBytes = Encoding.UTF8.GetBytes(plainText); + byte[] encryptedBytes = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length); + return Convert.ToBase64String(encryptedBytes); + } + } + + public static string Decrypt(string cipherText, string key) + { + byte[] keyBytes = Encoding.UTF8.GetBytes(key); + byte[] ivBytes = new byte[16]; // Initialization vector (IV) + + using (Aes aes = Aes.Create()) + { + aes.Key = keyBytes; + aes.IV = ivBytes; + ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV); + + byte[] cipherBytes = Convert.FromBase64String(cipherText); + byte[] decryptedBytes = decryptor.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length); + return Encoding.UTF8.GetString(decryptedBytes); + } + } + } + + + +}