zoukankan      html  css  js  c++  java
  • AES 加密位: 128位,加密模式:CBC, 填充模式:Zeros

    // AES 加密
            public byte[] AESEncrypt(string text)
            {
                byte[] data = Encoding.Unicode.GetBytes(text);
                SymmetricAlgorithm aes = Rijndael.Create();
                aes.Key = keyArray;
                aes.IV = keyArray;
                aes.Mode = CipherMode.CBC;
                aes.Padding = PaddingMode.Zeros;
    
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(data, 0, data.Length);
                        cs.FlushFinalBlock();
                        byte[] cipherBytes = ms.ToArray(); // 得到加密后的字节数组
                        cs.Close();
                        ms.Close();
                        aes.Clear();
    
                        return cipherBytes;
                    }
                }
            }

    // AES 解密
            public string AESDecrypt(byte[] data)
            {
                SymmetricAlgorithm aes = Rijndael.Create();
                aes.Key = keyArray;
                aes.IV = keyArray;
                aes.Mode = CipherMode.CBC;
                aes.Padding = PaddingMode.Zeros;
                byte[] decryptBytes = new byte[data.Length];
    
                using (MemoryStream ms = new MemoryStream(data))
                {
                    using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Read))
                    {
                        cs.Read(decryptBytes, 0, decryptBytes.Length);
                        cs.Close();
                        ms.Close();
                    }
                }
                aes.Clear();
    
                return System.Text.Encoding.Unicode.GetString(decryptBytes).Replace("", " ");
            }

  • 相关阅读:
    Lucene
    SQL优化以及索引
    Mysql优化
    RocketMQ
    RocketMQ
    Session共享
    Linux安装Nginx
    初识nginx
    跨域,防止表单重复提交
    HttpClient案例
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/6978403.html
Copyright © 2011-2022 走看看