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

      using System.Security.Cryptography;
    
        /// <summary>
        /// ASE加解密
        /// </summary>
        public class AESHelper
        {
            /// <summary>
            /// 获取密钥
            /// </summary>
            private static string Key
            {
                get
                {
                    return "秘钥";    ////必须是16位
                }
            }
            //默认密钥向量 
            private static byte[] _key1 = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
            /// <summary>
            /// AES加密算法
            /// </summary>
            /// <param name="text">明文字符串</param>
            /// <returns>将加密后的密文转换为Base64编码,以便显示</returns>
            public static string AESEncrypt(string text)
            {
                //分组加密算法
                SymmetricAlgorithm des = Rijndael.Create();
                byte[] inputByteArray = Encoding.UTF8.GetBytes(text);//得到需要加密的字节数组 
                //设置密钥及密钥向量
                des.Key = Encoding.UTF8.GetBytes(Key);
                des.IV = _key1;
                byte[] cipherBytes = null;
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, des.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 AESDecrypt(string passowrd)
            {
                byte[] cipherText = Convert.FromBase64String(passowrd);
                SymmetricAlgorithm des = Rijndael.Create();
                des.Key = Encoding.UTF8.GetBytes(Key);
                des.IV = _key1;
                byte[] decryptBytes = new byte[cipherText.Length];
                using (MemoryStream ms = new MemoryStream(cipherText))
                {
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Read))
                    {
                        cs.Read(decryptBytes, 0, decryptBytes.Length);
                        cs.Close();
                        ms.Close();
                    }
                }
                return Encoding.UTF8.GetString(decryptBytes).Replace("", "");   ///将字符串后尾的''去掉
            }
        }
  • 相关阅读:
    HDU 4165 pills
    POJ 2125 Destroy The Graph
    POJ 2075 Tangled in Cables
    NYOJ Number SequenceProblem F
    PHP Eval研究笔记
    PHP 学习备忘(更新中)
    iOS学习笔记1 版本控制
    PHP全局缓存上栽的坑及其他【反面教材系列】
    iOS学习笔记2 Code Format
    iOS学习笔记4 垃圾回收初涉
  • 原文地址:https://www.cnblogs.com/gossip/p/4913937.html
Copyright © 2011-2022 走看看