zoukankan      html  css  js  c++  java
  • C# 实现AES加密--解密

           /// <summary>
            ///  AES 加密
            /// </summary>
            /// <param name="str">明文(待加密)</param>
            /// <param name="key">密文</param>
            /// <returns></returns>
            public static string AesEncrypt(string str, string key)
            {
                if (string.IsNullOrEmpty(str)) return null;
                Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);
    
                System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
                {
                    Key = Encoding.UTF8.GetBytes(key),
                    Mode = System.Security.Cryptography.CipherMode.ECB,
                    Padding = System.Security.Cryptography.PaddingMode.PKCS7
                };
    
                System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
                Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
    
                return Convert.ToBase64String(resultArray, 0, resultArray.Length);
            }
            /// <summary>
            ///  AES 解密
            /// </summary>
            /// <param name="str">明文(待解密)</param>
            /// <param name="key">密文</param>
            /// <returns></returns>
            public static string AesDecrypt(string str, string key)
            {
                if (string.IsNullOrEmpty(str)) return null;
                Byte[] toEncryptArray = Convert.FromBase64String(str);
    
                System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
                {
                    Key = Encoding.UTF8.GetBytes(key),
                    Mode = System.Security.Cryptography.CipherMode.ECB,
                    Padding = System.Security.Cryptography.PaddingMode.PKCS7
                };
    
                System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateDecryptor();
                Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
    
                return Encoding.UTF8.GetString(resultArray);
            }

     欢迎评论。。。。让我看到你的反馈。。。。

  • 相关阅读:
    父子组件的数据传递
    前端处理:elementUI 表格索引代表第几条数据
    qt的moc,uic,rcc命令的使用
    支持无限精度无限大数的类BigNumber实现
    二进制的妙用
    C#实现任意大数的计算和简单逻辑命题的证明——前言
    打磨程序员的专属利器——文本
    打磨程序员的专属利器——快捷键
    打磨程序员的专属利器——命令行&界面
    Linux命令全集
  • 原文地址:https://www.cnblogs.com/rzm2wxm/p/7770810.html
Copyright © 2011-2022 走看看