• 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("", "");   //将字符串后尾的''去掉
            }

        }

  • 相关阅读:
    工作总结(二):Web Design
    工作总结(一):Linux C
    三十分钟学会AWK
    MySQL并发复制系列二:多线程复制 2016
    修改MySQL 5.7.9版本的root密码方法以及一些新变化整理
    sync_binlog innodb_flush_log_at_trx_commit 浅析
    MariaDB的"response time"插件
    Python学习九:列表生成式
    python中的深拷贝和浅拷贝理解
    Mycat 配置
  • 原文地址:https://www.cnblogs.com/sk2016/p/6088210.html
Copyright © 2020-2023  润新知