using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace OASystem.Domain.AesEncryption
{
///
/// Aes加密帮助类
///
public static class AesEncryptionHelper
{
private readonly static string Key = "FANMEIGUOJIKEY01"; // 16 字节密钥
private readonly static string IV = "FANMEIGUOJIIVV01"; // 16 字节向量
///
/// 加密
///
///
///
public static string Encrypt(string plainText)
{
if (string.IsNullOrEmpty(plainText)) return plainText;
if (EncryptionProcessor.IsEncrypted(plainText)) return plainText;
using var aes = Aes.Create();
aes.Key = Encoding.UTF8.GetBytes(Key);
aes.IV = Encoding.UTF8.GetBytes(IV);
using var encryptor = aes.CreateEncryptor();
var plainBytes = Encoding.UTF8.GetBytes(plainText);
var encryptedBytes = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length);
return Convert.ToBase64String(encryptedBytes);
}
///
/// 解密
///
///
///
public static string Decrypt(string cipherText)
{
if (string.IsNullOrEmpty(cipherText)) return cipherText;
if (!EncryptionProcessor.IsEncrypted(cipherText)) return cipherText;
using var aes = Aes.Create();
aes.Key = Encoding.UTF8.GetBytes(Key);
aes.IV = Encoding.UTF8.GetBytes(IV);
using var decryptor = aes.CreateDecryptor();
var cipherBytes = Convert.FromBase64String(cipherText);
var plainBytes = decryptor.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
return Encoding.UTF8.GetString(plainBytes);
}
}
}