| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Security.Cryptography;
- namespace XYFDRQ.DBUtility
- {
- /// <summary>
- /// AES加密解密
- /// </summary>
- public class AES
- {
- /// <summary>
- /// 获取密钥
- /// </summary>
- private static string Key
- {
- get { return @")O[NB]6,YF}+efcaj{+oESb9d8>Z'e9M"; }
- }
- /// <summary>
- /// 获取向量
- /// </summary>
- private static string IV
- {
- get { return @"L+\~f4,Ir)b$=pkf"; }
- }
- /// <summary>
- /// AES加密
- /// </summary>
- /// <param name="plainStr">明文字符串</param>
- /// <returns>密文</returns>
- public static string AESEncrypt(string plainStr)
- {
- byte[] bKey = Encoding.UTF8.GetBytes(Key);
- byte[] bIV = Encoding.UTF8.GetBytes(IV);
- byte[] byteArray = Encoding.UTF8.GetBytes(plainStr);
- string encrypt = null;
- Rijndael aes = Rijndael.Create();
- try
- {
- using (MemoryStream mStream = new MemoryStream())
- {
- using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write))
- {
- cStream.Write(byteArray, 0, byteArray.Length);
- cStream.FlushFinalBlock();
- encrypt = Convert.ToBase64String(mStream.ToArray());
- }
- }
- }
- catch { }
- aes.Clear();
- return encrypt;
- }
- /// <summary>
- /// AES加密
- /// </summary>
- /// <param name="plainStr">明文字符串</param>
- /// <param name="returnNull">加密失败时是否返回 null,false 返回 String.Empty</param>
- /// <returns>密文</returns>
- public static string AESEncrypt(string plainStr, bool returnNull)
- {
- string encrypt = AESEncrypt(plainStr);
- return returnNull ? encrypt : (encrypt == null ? String.Empty : encrypt);
- }
- /// <summary>
- /// AES解密
- /// </summary>
- /// <param name="encryptStr">密文字符串</param>
- /// <returns>明文</returns>
- public static string AESDecrypt(string encryptStr)
- {
- string decrypt = null;
- Rijndael aes = Rijndael.Create();
- try
- {
- byte[] bKey = Encoding.UTF8.GetBytes(Key);
- byte[] bIV = Encoding.UTF8.GetBytes(IV);
- byte[] byteArray = Convert.FromBase64String(encryptStr);
- using (MemoryStream mStream = new MemoryStream())
- {
- using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write))
- {
- cStream.Write(byteArray, 0, byteArray.Length);
- cStream.FlushFinalBlock();
- decrypt = Encoding.UTF8.GetString(mStream.ToArray());
- }
- }
- }
- catch { }
- aes.Clear();
- return decrypt;
- }
- /// <summary>
- /// AES解密
- /// </summary>
- /// <param name="encryptStr">密文字符串</param>
- /// <param name="returnNull">解密失败时是否返回 null,false 返回 String.Empty</param>
- /// <returns>明文</returns>
- public static string AESDecrypt(string encryptStr, bool returnNull)
- {
- string decrypt = AESDecrypt(encryptStr);
- return returnNull ? decrypt : (decrypt == null ? String.Empty : decrypt);
- }
- }
- }
|