zoukankan      html  css  js  c++  java
  • C#中AES加密和解密

    /// AES加密
            /// </summary>
            /// <param name="inputdata">输入的数据</param>
            /// <param name="iv">向量128位</param>
            /// <param name="strKey">加密密钥</param>
            /// <returns></returns>
            public static byte[] AESEncrypt(byte[] inputdata, byte[] iv, string strKey)
            {
                //分组加密算法   
                SymmetricAlgorithm des = Rijndael.Create();
                byte[] inputByteArray = inputdata;//得到需要加密的字节数组       
                //设置密钥及密钥向量
                des.Key = Encoding.UTF8.GetBytes(strKey.Substring(0, 32));
                des.IV = iv;
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(inputByteArray, 0, inputByteArray.Length);
                        cs.FlushFinalBlock();
                        byte[] cipherBytes = ms.ToArray();//得到加密后的字节数组   
                        cs.Close();
                        ms.Close();                   
                        return cipherBytes;
                    }
                }
            }
    
    
            /// <summary>
            /// AES解密
            /// </summary>
            /// <param name="inputdata">输入的数据</param>
            /// <param name="iv">向量128</param>
            /// <param name="strKey">key</param>
            /// <returns></returns>
            public static byte[] AESDecrypt(byte[] inputdata, byte[] iv, string strKey)
            {
                SymmetricAlgorithm des = Rijndael.Create();
                des.Key = Encoding.UTF8.GetBytes(strKey.Substring(0, 32));
                des.IV = iv;
                byte[] decryptBytes = new byte[inputdata.Length];
                using (MemoryStream ms = new MemoryStream(inputdata))
                {
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Read))
                    {
                        cs.Read(decryptBytes, 0, decryptBytes.Length);
                        cs.Close();
                        ms.Close();
                    }
                }
                return decryptBytes;
            }
  • 相关阅读:
    HTTP长连接、短连接使用及测试
    递归分治算法之二维数组二分查找(Java版本)
    Java二维数组的概念和使用方法
    java二维数组遍历
    HashMap多线程并发问题分析
    Linux 移动或重命名文件/目录-mv 的10个实用例子
    CSS fixed 定位元素失效的问题
    关于 JavaScript 中的继承
    React 虚拟 DOM 的差异检测机制
    下拉框中选项的快速定位
  • 原文地址:https://www.cnblogs.com/xuxiaoshuan/p/5485777.html
Copyright © 2011-2022 走看看