zoukankan      html  css  js  c++  java
  • AES加密解密

     public class AESDE
        {
            public AESDE() { }

            #region AES-BAIDU
            
            /// <summary>
            /// AES加密
            /// </summary>
            /// <param name="encryptStr">明文</param>
            /// <param name="key">密钥</param>
            /// <returns></returns>

            public static string Encrypt( string encryptStr, string key )
            {

                byte[] keyArray = UTF8Encoding.UTF8.GetBytes( key );

                byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes( encryptStr );

                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="decryptStr">密文</param>
            /// <param name="key">密钥</param>
            /// <returns></returns>

            public static string Decrypt( string decryptStr, string key )
            {

                byte[] keyArray = UTF8Encoding.UTF8.GetBytes( key );

                byte[] toEncryptArray = Convert.FromBase64String( decryptStr );

                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 );

            }

            #endregion
        }

  • 相关阅读:
    Windows下Goland的Terminal设置为Git Bash
    BoltDB简单使用教程
    Base64编码转换原理
    [区块链|非对称加密] 对数字证书(CA认证)原理的回顾
    [数据库锁机制] 深入理解乐观锁、悲观锁以及CAS乐观锁的实现机制原理分析
    升级mojave后的小问题解决
    ubuntu安装ssh服务记录
    dubbo+maven多模块项目单元测试
    sass与less
    (转)初识 Lucene
  • 原文地址:https://www.cnblogs.com/tonglei/p/4422854.html
Copyright © 2011-2022 走看看