zoukankan      html  css  js  c++  java
  • 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;
                }
            }
        }
    }
  • 相关阅读:
    bzoj3884: 上帝与集合的正确用法(数论)
    洛谷10月月赛R2·浴谷八连测R3题解
    bzoj5055: 膜法师(BIT)
    bzoj2213: [Poi2011]Difference(思维题)
    bzoj1016: [JSOI2008]最小生成树计数(kruskal+dfs)
    一模 (2) day2
    一模 (2) day1
    Prime Palindromes
    常州培训 day5 解题报告
    一模 (1) day2
  • 原文地址:https://www.cnblogs.com/zhuwansu/p/10836960.html
Copyright © 2011-2022 走看看