• AES 加密与解密


    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Security.Cryptography;
    using System.Text;
    using System.Web;

    namespace WeChat.AES
    {
    public class AESHelper
    {
    /// <summary>
    /// 获取密钥
    /// </summary>
    private static string key
    {
    get
    {
    return "qaz14789wsxedcrf"; ////必须是16位
    }
    }

    /// <summary>
    /// AES 加密
    /// </summary>
    /// <param name="str">明文(待加密)</param>
    /// <param name="key">密文</param>
    /// <returns></returns>
    public static string AesEncrypt(string str)
    {
    if (string.IsNullOrEmpty(str)) return null;
    Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);

    RijndaelManaged rm = new RijndaelManaged
    {
    Key = Encoding.UTF8.GetBytes(key),
    Mode = CipherMode.ECB,
    Padding = PaddingMode.PKCS7
    };

    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)
    {
    if (string.IsNullOrEmpty(str)) return null;
    Byte[] toEncryptArray = Convert.FromBase64String(str);

    RijndaelManaged rm = new RijndaelManaged
    {
    Key = Encoding.UTF8.GetBytes(key),
    Mode = CipherMode.ECB,
    Padding = PaddingMode.PKCS7
    };

    ICryptoTransform cTransform = rm.CreateDecryptor();
    Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

    return Encoding.UTF8.GetString(resultArray);
    }
    }
    }

  • 相关阅读:
    普通索引和唯一索引的选择
    深入理解MySQL索引(上)
    深入理解MySQL索引(下)
    Python3爬取小说并保存到文件
    MySQL45讲:一条update语句是怎样执行的
    IO软件层次结构与假脱机技术
    一条查询SQl是怎样执行的
    MySQL45讲笔记-事务隔离级别,为什么你改了数据我看不见
    了解一下IO控制器与控制方式
    3.6类别不平衡问题
  • 原文地址:https://www.cnblogs.com/zoujinhua/p/10483505.html
Copyright © 2020-2023  润新知