zoukankan      html  css  js  c++  java
  • Asp.Net C# AES加密、解密

    Asp.Net C#续上次的DES加密、解密之后,再发一个AES的加密、解密。AES要注意的是32位密匙。

    private static readonly String strAesKey = "iwww.maoblog.comiwww.maoblog.com";//加密所需32位密匙

    /// <summary>
    /// AES加密
    /// </summary>
    /// <param name="str">要加密字符串</param>
    /// <returns>返回加密后字符串</returns>
    public static String Encrypt_AES(String str)
    {
        Byte[] keyArray = System.Text.UTF8Encoding.UTF8.GetBytes(strAesKey);
        Byte[] toEncryptArray = System.Text.UTF8Encoding.UTF8.GetBytes(str);

        System.Security.Cryptography.RijndaelManaged rDel = new System.Security.Cryptography.RijndaelManaged();
        rDel.Key = keyArray;
        rDel.Mode = System.Security.Cryptography.CipherMode.ECB;
        rDel.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

        System.Security.Cryptography.ICryptoTransform cTransform = rDel.CreateEncryptor();
        Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

        return Convert.ToBase64String(resultArray, 0, resultArray.Length);
    }

    /// <summary>
    /// AES解密
    /// </summary>
    /// <param name="str">要解密字符串</param>
    /// <returns>返回解密后字符串</returns>
    public static String Decrypt_AES(String str)
    {
        Byte[] keyArray = System.Text.UTF8Encoding.UTF8.GetBytes(strAesKey);
        Byte[] toEncryptArray = Convert.FromBase64String(str);

        System.Security.Cryptography.RijndaelManaged rDel = new System.Security.Cryptography.RijndaelManaged();
        rDel.Key = keyArray;
        rDel.Mode = System.Security.Cryptography.CipherMode.ECB;
        rDel.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

        System.Security.Cryptography.ICryptoTransform cTransform = rDel.CreateDecryptor();
        Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

        return System.Text.UTF8Encoding.UTF8.GetString(resultArray);
    }

  • 相关阅读:
    Django——基于类的视图源码分析 三
    python——深刻理解Python中的元类(metaclass)
    Django——静态文件配置
    Django——如何使用Template以及如何向template传递变量
    Django—— 缓存框架
    Django——META内部类选项
    Django——20141014深入理解Django HttpRequest HttpResponse的类和实例
    Django——如何在Django模板中注入全局变量?——part2
    Mysql查找如何判断字段是否包含某个字符串
    Mysql分表和分区的区别
  • 原文地址:https://www.cnblogs.com/kiwifruit/p/1863141.html
Copyright © 2011-2022 走看看