AESEncryption Aes 加密

Posted zhuwansu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AESEncryption Aes 加密相关的知识,希望对你有一定的参考价值。

/*******************************************************
 * 
 * 作者:朱皖苏
 * 创建日期:20180521
 * 说明:此文件只包含一个类,具体内容见类型注释。
 * 运行环境:.NET 4.0
 * 版本号:1.0.0
 * 
 * 历史记录:
 * 创建文件 朱皖苏 20180521 20:08
 * 
*******************************************************/

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Dben.CommonLib.Cryptography
{
    /// <summary>
    /// Aes 加密
    /// </summary>
    public class AESEncryption
    {
        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="encryptString">待加密字符串</param>
        /// <param name="key">16位密钥</param>
        /// <returns></returns>
        public static string EncryptAes(string encryptString, string key)
        {
            try
            {
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                SymmetricAlgorithm des = Aes.Create();
                des.Key = Encoding.ASCII.GetBytes(key.Substring(0, 16));

                des.IV = Encoding.ASCII.GetBytes(key.Substring(16));
                des.Mode = CipherMode.CBC;
                des.Padding = PaddingMode.Zeros;

                using (MemoryStream mStream = new MemoryStream())
                {
                    using (CryptoStream cStream = new CryptoStream(mStream, des.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cStream.Write(inputByteArray, 0, inputByteArray.Length);
                        cStream.FlushFinalBlock();

                        byte[] desBytes = mStream.ToArray();
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < desBytes.Length; i++)
                        {
                            sb.Append(desBytes[i].ToString("x2"));
                        }
                        return sb.ToString();
                    }
                }
            }
            catch (Exception)
            {
                return encryptString;
            }

        }

        /// <summary>
        /// AES解密 
        /// </summary>
        /// <param name="decryptString">解密字符串</param>
        /// <param name="key">密钥</param>
        /// <returns></returns>
        public static string DecryptAes(string decryptString, string key)
        {
            try
            {
                byte[] inputByteArray = StrToToHexByte(decryptString);
                SymmetricAlgorithm des = Aes.Create();
                des.Key = Encoding.ASCII.GetBytes(key.Substring(0, 16));

                des.IV = Encoding.ASCII.GetBytes(key.Substring(16));
                des.Padding = PaddingMode.Zeros;
                des.Mode = CipherMode.CBC;
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, des.CreateDecryptor(), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                byte[] desDecryBytes = mStream.ToArray();
                return Encoding.UTF8.GetString(desDecryBytes);
            }
            catch (Exception)
            {
                return decryptString;
            }
        }

        /// <summary>
        /// 转16进制字符串
        /// </summary>
        /// <param name="hexString">待转换字符串</param>
        /// <returns></returns>
        private static byte[] StrToToHexByte(string hexString)
        {
            try
            {
                hexString = hexString.Replace(" ", "");
                if ((hexString.Length % 2) != 0)
                    hexString += " ";
                byte[] returnBytes = new byte[hexString.Length / 2];
                for (int i = 0; i < returnBytes.Length; i++)
                    returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
                return returnBytes;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}

 

以上是关于AESEncryption Aes 加密的主要内容,如果未能解决你的问题,请参考以下文章

AES 加密 - 大文件

AES加密,啥是公钥和私钥?

AES 加密 - 密钥与 IV

python实现AES加密解密

aes加密算法C代码

更新实现代码对称加密与解密剖析:AES,高级加密标准(Advanced Encryption Standard,缩写AES)