//默认密钥向量
        private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        /// <summary>
        /// 默认密钥
        /// </summary>
        private static string miyue = "09@/*!^-+123";


        #region DES加密字符串

        /// <summary>
        /// DES加密字符串
        /// </summary>
        /// <param name="encryptString">待加密的字符串</param>
        /// <param name="encryptKey">加密密钥,要求为8位</param>
        /// <returns>加密成功返回加密后的16进制字符串,失败返回源串</returns>
        public static string EncryptDES(string encryptString)
        {
            try
            {
                //string encryptKey加密密钥参数,我将该参数这里去掉了,用了默认密钥
                //byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                byte[] rgbKey = Encoding.UTF8.GetBytes(miyue);//加密密钥          
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                byte[] tembyte = mStream.ToArray();
                StringBuilder sb = new StringBuilder();
                foreach (byte item in tembyte)
                {
                    sb.AppendFormat("{0:x2}", item);
                }
                return sb.ToString();
            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion

        #region DES解密字符串

        /// <summary>
        /// DES解密字符串
        /// </summary>
        /// <param name="decryptString">待解密的16进制字符串</param>
        /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
        /// <returns>解密成功返回解密后的字符串,失败返源串</returns>
        public static string DecryptDES(string decryptString)
        {
            try
            {
                //string decryptKey解密密钥参数,我将该参数这里去掉了,用了默认密钥
                //byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
                byte[] rgbKey = Encoding.UTF8.GetBytes(miyue);//解密密钥          
                byte[] rgbIV = Keys;
                decryptString = decryptString.Replace(" ", "");
                decryptString = decryptString.Replace("\r\n", "");
                string txt16 = decryptString;
                byte[] inputByteArray = new byte[txt16.Length / 2];
                for (int i = 0; i < inputByteArray.Length; i++)
                {
                    inputByteArray[i] = Convert.ToByte(txt16.Substring(i * 2, 2), 16);
                }
                DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Encoding.UTF8.GetString(mStream.ToArray());
            }
            catch (Exception)
            {
                throw;
            }
        } 
        #endregion
        #region AES加密
        /// <summary>    
        /// AES加密
        /// </summary>    
        /// <param name="text">被加密的明文</param>    
        /// <param name="key">密钥</param>    
        /// <returns>密文</returns>    
        public static string AESEncrypt(string text, string key)
        {
            string iv = "789!@#$%^&*()253LKJHGATGKGK/asdfsdq#$werasdfhhh";//默认向量
            RijndaelManaged rijndaelCipher = new RijndaelManaged();
            rijndaelCipher.Mode = CipherMode.CBC;
            rijndaelCipher.Padding = PaddingMode.PKCS7;
            rijndaelCipher.KeySize = 256;
            rijndaelCipher.BlockSize = 256;
            Byte[] bKey = new Byte[32];
            Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length);
            Byte[] bVector = new Byte[32];
            Array.Copy(Encoding.UTF8.GetBytes(iv.PadRight(bVector.Length)), bVector, bVector.Length);
            rijndaelCipher.Key = bKey;
            rijndaelCipher.IV = bVector;
            ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
            byte[] encryptedData = Encoding.UTF8.GetBytes(text);
            byte[] cipherBytes = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
            return Convert.ToBase64String(cipherBytes);
        }

        /// <summary>    
        /// AES解密
        /// </summary>    
        /// <param name="text">被加密的明文</param>    
        /// <param name="key">密钥</param>    
        /// <returns>明文</returns>    
        public static string AESDecrypt(string text, string key)
        {
            string iv = "789!@#$%^&*()253LKJHGATGKGK/asdfsdq#$werasdfhhh";//默认向量
            RijndaelManaged rijndaelCipher = new RijndaelManaged();
            rijndaelCipher.Mode = CipherMode.CBC;
            rijndaelCipher.Padding = PaddingMode.PKCS7;
            rijndaelCipher.KeySize = 256;
            rijndaelCipher.BlockSize = 256;

            byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(key);
            Byte[] bKey = new Byte[32];
            Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length);
            Byte[] bVector = new Byte[32];
            Array.Copy(Encoding.UTF8.GetBytes(iv.PadRight(bVector.Length)), bVector, bVector.Length);

            rijndaelCipher.Key = bKey;
            rijndaelCipher.IV = bVector;
            ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
            byte[] decryData = Convert.FromBase64String(text);
            byte[] plainText = transform.TransformFinalBlock(decryData, 0, decryData.Length);
            return Encoding.UTF8.GetString(plainText);
        }
        #endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace AES_WINFORM
{
    //将AES加密成16进制字符串
    public class AesHelpter
    {
        /// <summary>
        /// 默认密钥向量IV
        /// </summary>
        private readonly static string _iv = "1462#¥@/*op";
        /// <summary>
        /// 默认密钥向量KEY
        /// </summary>
        private readonly static string _key = "asldf#$%HJ@/g*jk";
//private readonly static string _iv = "789!@#WER$%^&*()GKMS/IMG8934q#$wased.gisl";
//private readonly static string _key = "@#sldfj$%6798067*()-*/+lsajdfADKM";

        #region AES加密

        /// <summary>    
        /// AES加密
        /// </summary>    
        /// <param name="text">被加密的明文</param>    
        /// <param name="key">密钥</param>    
        /// <returns>密文</returns>    
        public static string AESEncrypt(string text)
        {
            RijndaelManaged rijndaelCipher = new RijndaelManaged();
            rijndaelCipher.Mode = CipherMode.CBC;
            rijndaelCipher.Padding = PaddingMode.PKCS7;
            rijndaelCipher.KeySize = 256;
            rijndaelCipher.BlockSize = 256;
            Byte[] bKey = new Byte[32];
            Array.Copy(Encoding.UTF8.GetBytes(_key.PadRight(bKey.Length)), bKey, bKey.Length);
            Byte[] bVector = new Byte[32];
            Array.Copy(Encoding.UTF8.GetBytes(_iv.PadRight(bVector.Length)), bVector, bVector.Length);
            rijndaelCipher.Key = bKey;
            rijndaelCipher.IV = bVector;
            ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
            byte[] encryptedData = Encoding.UTF8.GetBytes(text);
            byte[] cipherBytes = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
            //转换为16进制字符串
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < cipherBytes.LongLength; i++)
            {
                sb.Append(cipherBytes[i].ToString("X2"));
            }
            return sb.ToString();
        }

        /// <summary>    
        /// AES解密
        /// </summary>    
        /// <param name="text">被加密的明文</param>    
        /// <param name="key">密钥</param>    
        /// <returns>明文</returns>    
        public static string AESDecrypt(string decryptString)
        {
            RijndaelManaged rijndaelCipher = new RijndaelManaged();
            rijndaelCipher.Mode = CipherMode.CBC;
            rijndaelCipher.Padding = PaddingMode.PKCS7;
            rijndaelCipher.KeySize = 256;
            rijndaelCipher.BlockSize = 256;

            decryptString = decryptString.Replace(" ", "");
            decryptString = decryptString.Replace(System.Environment.NewLine, "");
            string txt16 = decryptString;
            byte[] inputByteArray = new byte[txt16.Length / 2];
            for (int i = 0; i < inputByteArray.Length; i++)
            {
                inputByteArray[i] = Convert.ToByte(txt16.Substring(i * 2, 2), 16);
            }
            byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(_key);
            Byte[] bKey = new Byte[32];
            Array.Copy(Encoding.UTF8.GetBytes(_key.PadRight(bKey.Length)), bKey, bKey.Length);
            Byte[] bVector = new Byte[32];
            Array.Copy(Encoding.UTF8.GetBytes(_iv.PadRight(bVector.Length)), bVector, bVector.Length);

            rijndaelCipher.Key = bKey;
            rijndaelCipher.IV = bVector;
            ICryptoTransform transform = rijndaelCipher.CreateDecryptor();     
            byte[] plainText = transform.TransformFinalBlock(inputByteArray, 0, inputByteArray.Length);
            return Encoding.UTF8.GetString(plainText);
        }

        #endregion
    }
}

本文转载:CSDN博客