zoukankan      html  css  js  c++  java
  • AES加解密

     public class AESHelper
        {
            /// <summary>
            /// 获取密钥
            /// </summary>
            private static string IDKey
            {
                get
                {
                    return "3vZ8rRLUME4b3K8xXovptGodOUXSCjrc";
                }
            }
    
            private static string nameKey
            {
    
                get
                {
                    return "1a54CRZ7WdwGYdNPonWkSMtHyRhlnv6q"; 
                }
            }
    
            //默认密钥向量
            private static byte[] _key1 = { 0x7f, 0x0a, 0x2d, 0x96, 0x94, 0xa5, 0xc2, 0x7b, 0xaa, 0x89, 0x00, 0x8b, 0xf3, 0xab, 0x15, 0xfd };
          /// <summary>
            /// AES加密算法
            /// </summary>
            /// <param name="plainText">明文字符串</param>
            /// <returns>将加密后的密文转换为Base64编码,以便显示</returns>
            public static string AESEncryptID(string plainText)
            {
                //分组加密算法
                SymmetricAlgorithm aes = Rijndael.Create();
                byte[] inputByteArray = Encoding.UTF8.GetBytes(plainText);//得到需要加密的字节数组
                //设置密钥及密钥向量
                aes.Key = Encoding.UTF8.GetBytes(IDKey);
                aes.IV = _key1;
                byte[] cipherBytes = null;
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(inputByteArray, 0, inputByteArray.Length);
                        cs.FlushFinalBlock();
                        cipherBytes = ms.ToArray();//得到加密后的字节数组
                        cs.Close();
                        ms.Close();
                    }
                }
                return Convert.ToBase64String(cipherBytes);
            }
    
            public static string AESEncryptName(string plainText)
            {
                //分组加密算法
                SymmetricAlgorithm aes = Rijndael.Create();
                byte[] inputByteArray = Encoding.UTF8.GetBytes(plainText);//得到需要加密的字节数组
                //设置密钥及密钥向量
                aes.Key = Encoding.UTF8.GetBytes(nameKey);
                aes.IV = _key1;
                byte[] cipherBytes = null;
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(inputByteArray, 0, inputByteArray.Length);
                        cs.FlushFinalBlock();
                        cipherBytes = ms.ToArray();//得到加密后的字节数组
                        cs.Close();
                        ms.Close();
                    }
                }
                return Convert.ToBase64String(cipherBytes);
            }
    
            /// <summary>
            /// AES解密
            /// </summary>
            /// <param name="cipherText">密文字符串</param>
            /// <returns>返回解密后的明文字符串</returns>
            public static string AESDecryptId(string showText)
            {
                byte[] cipherText = Convert.FromBase64String(showText);
    
                SymmetricAlgorithm aes = Rijndael.Create();
                aes.Key = Encoding.UTF8.GetBytes(IDKey);
                aes.IV = _key1;
                byte[] decryptBytes = new byte[cipherText.Length];
                using (MemoryStream ms = new MemoryStream(cipherText))
                {
                    using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Read))
                    {
                        cs.Read(decryptBytes, 0, decryptBytes.Length);
                        cs.Close();
                        ms.Close();
                    }
                }
                return Encoding.UTF8.GetString(decryptBytes).Replace("", "");   ///将字符串后尾的''去掉
            }
    
            public static string AESDecryptName(string showText)
            {
                byte[] cipherText = Convert.FromBase64String(showText);
    
                SymmetricAlgorithm aes = Rijndael.Create();
                aes.Key = Encoding.UTF8.GetBytes(nameKey);
                aes.IV = _key1;
                byte[] decryptBytes = new byte[cipherText.Length];
                using (MemoryStream ms = new MemoryStream(cipherText))
                {
                    using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Read))
                    {
                        cs.Read(decryptBytes, 0, decryptBytes.Length);
                        cs.Close();
                        ms.Close();
                    }
                }
                return Encoding.UTF8.GetString(decryptBytes).Replace("", "");   ///将字符串后尾的''去掉
            }
    
            public static void genKey()
            {
    
                var generator = new RijndaelManaged();
                var key = Convert.ToBase64String(generator.Key);
                byte[] iv = generator.IV;
    
                Console.WriteLine(key);
                Console.WriteLine(generator.IV);
            }
        }

       Key:为32位的字符

     byte:0-255之间的16进制数字

  • 相关阅读:
    开始学习编写用于 Windows SideShow 设备的小工具【转】
    Windows Mobile 6.5 Developer Tool Kit 下载
    Microsoft Security Essentials 微软免费杀毒软件下载
    SQL Server 2008 空间数据存储摘抄(SRID 点 MultiPoint LineString MultiLineString 多边形 MultiPolygon GeometryCollection)
    Vista Sidebar Gadget (侧边栏小工具)开发教程 (2)
    Vista Sidebar Gadget (侧边栏小工具)开发教程 (4)
    负载测试、压力测试和性能测试的异同
    Windows Server 2008 Vista Sidebar Gadget (侧边栏小工具) 入门开发实例
    Silverlight Tools 安装失败 解决办法
    SQL Server 2008 空间数据库 空间索引概念及创建(取自帮助)
  • 原文地址:https://www.cnblogs.com/25miao/p/6867798.html
Copyright © 2011-2022 走看看