Aes 加解密算法

public class AesHelper
    {
        /// <summary>
        /// 生成128位的随机AES秘钥
        /// </summary>
        public static void AutoCreateAesKey()
        {
            AesCryptoServiceProvider m_aesCryptoServiceProvider = new AesCryptoServiceProvider();
            m_aesCryptoServiceProvider.KeySize = 128;
            m_aesCryptoServiceProvider.GenerateKey();
            byte[] key = m_aesCryptoServiceProvider.Key;
            var strKey = Convert.ToBase64String(key, 0, key.Length);
        }

        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="prestr"></param>
        /// <param name="keyArray"></param>
        /// <returns></returns>
        public static string Encrypt(string prestr, byte[] keyArray)
        {

            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(prestr);

            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = CipherMode.ECB;
            rDel.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = rDel.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="prestr"></param>
        /// <param name="keyArray"></param>
        /// <returns></returns>
        public static string Decrypt(string prestr, byte[] keyArray)
        {
            byte[] toEncryptArray = Convert.FromBase64String(prestr);
            // byte[] keysss = Convert.FromBase64String(aeskey);
            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = keyArray;

            rDel.Mode = CipherMode.ECB;
            rDel.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = rDel.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return UTF8Encoding.UTF8.GetString(resultArray);
        }

        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="prestr">待加密串</param>
        /// <param name="key">密钥</param>
        /// <returns></returns>
        public static string Decrypt(string prestr, string key)
        {
            byte[] toEncryptArray = Convert.FromBase64String(prestr);
            byte[] keyArray = Convert.FromBase64String(key);
            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = keyArray;

            rDel.Mode = CipherMode.ECB;
            rDel.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = rDel.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return UTF8Encoding.UTF8.GetString(resultArray);
        }
    }

public static class SlAes
    {
        //默认密钥向量
        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="plainText">明文字符串</param>
        /// <param name="key">密钥</param>
        /// <param name="endcoding">编码格式</param>
        /// <returns>将加密后的密文转换为Base64编码,以便显示</returns>
        public static string Encrypt(string plainText, string key,Encoding endcoding=null)
        {
            endcoding = endcoding ?? SlEncoding.Default;
            //分组加密算法
            SymmetricAlgorithm des = Rijndael.Create();
            byte[] inputByteArray = endcoding.GetBytes(plainText);//得到需要加密的字节数组
            //设置密钥及密钥向量
            des.Key = endcoding.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>
        /// <param name="key">密钥</param>
        /// <param name="encoding">编码格式</param>
        /// <returns>返回解密后的明文字符串</returns>
        public static string Decrypt(string cipherText, string key, Encoding encoding = null)
        {
            encoding = encoding ?? SlEncoding.Default;

            byte[] byteCipherText = Convert.FromBase64String(cipherText);
            SymmetricAlgorithm des = Rijndael.Create();
            des.Key = encoding.GetBytes(key);
            des.IV = _key1;
            byte[] decryptBytes = new byte[cipherText.Length];
            using (MemoryStream ms = new MemoryStream(byteCipherText))
            {
                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("\0", "");   //将字符串后尾的'\0'去掉
        }

    }

上一篇:day8_python学习笔记_chapter11_函数


下一篇:M - 非诚勿扰 优先队列