zoukankan      html  css  js  c++  java
  • C#使用AES加密

        public class AES
        {
            /// <summary>
            /// 获取密钥 必须是32字节
            /// </summary>
            private static string Key
            {
                get { return @"ae125efkk4_54eeff444ferfkny6oxi8"; }
            }
    
            /// <summary>
            /// AES加密
            /// </summary>
            /// <param name="plainStr">明文字符串</param>
            /// <returns>密文</returns>
            public static string AESEncrypt(string encryptStr)
            {
                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);
            }
            public static string AESDEncrypt(string encryptStr)
            {
                byte[] keyArray = UTF8Encoding.UTF8.GetBytes(Key);
                byte[] toEncryptArray = Convert.FromBase64String(encryptStr);
                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);
            }
        }

      调用代码:

            static void Main(string[] args)
            {
    
                string s = AES.AESEncrypt("我1爱小23小张0-0");
                Console.WriteLine(s);
                Console.WriteLine(AES.AESDEncrypt(s));
                Console.Read();
            }

     

    Hold on, everything is possible.
  • 相关阅读:
    如何为基于windows验证的站点的某个页面、文件或文件夹单独设置匿名访问
    JMeter基础
    selenium select操作及其他补充
    HTML面试
    第一天
    AspNetPager 7.4.2版发布
    java移位运算copy来的。为了备忘
    android 开发中的错误
    启动android模拟器报错
    最省内存的方式加载图片资源
  • 原文地址:https://www.cnblogs.com/student-note/p/6862072.html
Copyright © 2011-2022 走看看