hang_S_1_可逆加密算法增加

Uat_Study
hang 2023-07-04 15:24:06 +08:00
parent 6f556c45df
commit 516f0fa1b4
1 changed files with 46 additions and 0 deletions

View File

@ -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);
}
}
}
}