12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Security.Cryptography;
- using System.Text;
- using System.Threading.Tasks;
- namespace OASystem.Domain.AesEncryption
- {
- /// <summary>
- /// Aes加密帮助类
- /// </summary>
- public static class AesEncryptionHelper
- {
- private readonly static string Key = "FANMEIGUOJIKEY01"; // 16 字节密钥
- private readonly static string IV = "FANMEIGUOJIIVV01"; // 16 字节向量
- /// <summary>
- /// 加密
- /// </summary>
- /// <param name="plainText"></param>
- /// <returns></returns>
- 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);
- }
- /// <summary>
- /// 解密
- /// </summary>
- /// <param name="cipherText"></param>
- /// <returns></returns>
- 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);
- }
- }
- }
|