hang_S_1_可逆加密算法增加
parent
cb096a1d9b
commit
0208edb9ed
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue