zoukankan      html  css  js  c++  java
  • C# AES 128加密,解密示例

            /// <summary>
            /// AES加密
            /// </summary>
            /// <param name="clearTxt"></param>
            /// <returns></returns>
            public static string AesEncrypt(string clearTxt)
            {
                string secretKey = "I15TMSLO0KXUWTHO";
    
                byte[] keyBytes = Encoding.UTF8.GetBytes(secretKey);
    
                using (RijndaelManaged cipher = new RijndaelManaged())
                {
                    cipher.Mode = CipherMode.CBC;
                    cipher.Padding = PaddingMode.PKCS7;
                    cipher.KeySize = 128;
                    cipher.BlockSize = 128;
                    cipher.Key = keyBytes;
                    cipher.IV = keyBytes;
    
                    byte[] valueBytes = Encoding.UTF8.GetBytes(clearTxt);
    
                    byte[] encrypted;
                    using (ICryptoTransform encryptor = cipher.CreateEncryptor())
                    {
                        using (MemoryStream ms = new MemoryStream())
                        {
                            using (CryptoStream writer = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
                            {
                                writer.Write(valueBytes, 0, valueBytes.Length);
                                writer.FlushFinalBlock();
                                encrypted = ms.ToArray();
    
                                StringBuilder sb = new StringBuilder();
                                for (int i = 0; i < encrypted.Length; i++)
                                    sb.Append(Convert.ToString(encrypted[i], 16).PadLeft(2, '0'));
                                return sb.ToString().ToUpperInvariant();
                            }
                        }
                    }
                }
            }
    
            /// <summary>
            /// AES解密
            /// </summary>
            /// <param name="encrypted"></param>
            /// <returns></returns>
            public static string AesDecypt(string encrypted)
            {
                string secretKey = "I15TMSLO0KXUWTHO";
    
                byte[] keyBytes = Encoding.UTF8.GetBytes(secretKey);
    
                using (RijndaelManaged cipher = new RijndaelManaged())
                {
                    cipher.Mode = CipherMode.CBC;
                    cipher.Padding = PaddingMode.PKCS7;
                    cipher.KeySize = 128;
                    cipher.BlockSize = 128;
                    cipher.Key = keyBytes;
                    cipher.IV = keyBytes;
    
                    List<byte> lstBytes = new List<byte>();
                    for (int i = 0; i < encrypted.Length; i += 2)
                        lstBytes.Add(Convert.ToByte(encrypted.Substring(i, 2), 16));
    
                    using (ICryptoTransform decryptor = cipher.CreateDecryptor())
                    {
                        using (MemoryStream msDecrypt = new MemoryStream(lstBytes.ToArray()))
                        {
                            using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                            {
                                using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                                {
                                    return srDecrypt.ReadToEnd();
                                }
                            }
                        }
                    }
                }
            }
  • 相关阅读:
    fmri降噪,利用spatial+temporal信息
    matlab中,计算,记录,程序运行,起始,结束 时间,间隔 &matlab中 tic,toc函数的用法
    第十五章 动态规划——矩阵链乘法
    第十五章 动态规划——钢条切割
    第十四章 数据结构的扩张
    第十四章 红黑树——C++代码实现
    第十三章 红黑树
    第十二章 二叉搜索树
    第十一章 散列表
    第十章 基本数据结构——二叉树
  • 原文地址:https://www.cnblogs.com/xyz0835/p/5775850.html
Copyright © 2011-2022 走看看