加密算法AES

Posted weiweixiang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了加密算法AES相关的知识,希望对你有一定的参考价值。

这里不讲原理,只有实现的简单封装

/// <summary>
/// AES加解密
/// </summary>
public class AesHelper
{
    //默认密钥、初始化向量
    private static readonly string _Key = "Q3zmD8v^LWYu5QsX";
    private static readonly string _Vector = "figQi#gxIxqcnaKr";

    /// <summary>
    /// AES加密
    /// 密码:默认
    /// 偏移量:默认
    /// 模式:CBC
    /// 填充:PKCS7
    /// 输出:Base64
    /// 编码:UTF8
    /// </summary>
    /// <param name="plaintext">明文</param>
    /// <returns>大写的密文</returns>
    public static string Encrypt(string plaintext)
    {
        return Encrypt(plaintext, _Key, _Vector, OutputMethod.Base64, CipherMode.CBC, PaddingMode.PKCS7);
    }

    /// <summary>
    /// AES加密
    /// 密码:默认
    /// 偏移量:默认
    /// 模式:CBC
    /// 填充:PKCS7
    /// 输出:自定义
    /// 编码:UTF8
    /// </summary>
    /// <param name="plaintext">明文</param>
    /// <returns>大写的密文</returns>
    public static string Encrypt(string plaintext, OutputMethod method)
    {
        return Encrypt(plaintext, _Key, _Vector, method, CipherMode.CBC, PaddingMode.PKCS7);
    }

    /// <summary>
    /// AES加密
    /// 密码:自定义
    /// 偏移量:自定义
    /// 模式:CBC
    /// 填充:PKCS7
    /// 输出:Base64
    /// 编码:UTF8
    /// </summary>
    /// <param name="plaintext">明文</param>
    /// <param name="key">密码,16位,不含中文</param>
    /// <param name="iv">偏移量,16位,不含中文</param>
    /// <returns></returns>
    public static string Encrypt(string plaintext, string key, string iv)
    {
        return Encrypt(plaintext, key, iv, OutputMethod.Base64, CipherMode.CBC, PaddingMode.PKCS7);
    }

    /// <summary>
    /// AES加密
    /// 密码:自定义
    /// 偏移量:自定义
    /// 模式:CBC
    /// 填充:PKCS7
    /// 输出:自定义
    /// 编码:UTF8
    /// </summary>
    /// <param name="plaintext">明文</param>
    /// <param name="key">密码,16位,不含中文</param>
    /// <param name="iv">偏移量,16位,不含中文</param>
    /// <returns></returns>
    public static string Encrypt(string plaintext, string key, string iv, OutputMethod method)
    {
        return Encrypt(plaintext, key, iv, method, CipherMode.CBC, PaddingMode.PKCS7);
    }

    /// <summary>
    /// AES加密
    /// 密码:自定义
    /// 偏移量:自定义
    /// 模式:自定义
    /// 填充:自定义
    /// 输出:自定义
    /// 编码:UTF8
    /// </summary>
    /// <param name="plaintext">明文</param>
    /// <param name="key">密码,16位,不含中文</param>
    /// <param name="iv">偏移量,16位,不含中文</param>
    /// <param name="cipher">加密模式</param>
    /// <param name="padding">填充</param>
    /// <returns></returns>
    public static string Encrypt(string plaintext, string key, string iv, OutputMethod method, CipherMode cipher, PaddingMode padding)
    {
        if (String.IsNullOrEmpty(plaintext))
        {
            throw new ArgumentNullException("明文不能为空");
        }

        if (String.IsNullOrEmpty(key) || key.Length != 16)
        {
            throw new ArgumentNullException("密钥参数错误");
        }

        if (String.IsNullOrEmpty(iv) || key.Length != 16)
        {
            throw new ArgumentNullException("向量参数错误");
        }

        byte[] byteArray = Encoding.UTF8.GetBytes(plaintext);

        Rijndael rjd = Rijndael.Create();
        rjd.Key = Encoding.ASCII.GetBytes(key);
        rjd.IV = Encoding.ASCII.GetBytes(iv);
        rjd.Mode = cipher;
        rjd.Padding = padding;

        using (MemoryStream ms = new MemoryStream())
        {
            CryptoStream cStream = new CryptoStream(ms, rjd.CreateEncryptor(), CryptoStreamMode.Write);
            cStream.Write(byteArray, 0, byteArray.Length);
            cStream.FlushFinalBlock();

            byte[] result = ms.ToArray();
            return EadUtil.Output(result, method);
        }
    }

    /// <summary>
    /// AES解密
    /// 密码:默认
    /// 偏移量:默认
    /// 模式:CBC
    /// 填充:PKCS7
    /// 编码:UTF8
    /// </summary>
    /// <param name="plaintext">密文</param>
    /// <returns>返回明文</returns>
    public static string Decrypt(string plaintext)
    {
        return Decrypt(plaintext, _Key, _Vector, OutputMethod.Base64, CipherMode.CBC, PaddingMode.PKCS7);
    }

    /// <summary>
    /// AES解密
    /// 密码:默认
    /// 偏移量:默认
    /// 模式:CBC
    /// 填充:PKCS7
    /// 编码:UTF8
    /// </summary>
    /// <param name="plaintext">密文</param>
    /// <returns>返回明文</returns>
    public static string Decrypt(string plaintext, OutputMethod method)
    {
        return Decrypt(plaintext, _Key, _Vector, method, CipherMode.CBC, PaddingMode.PKCS7);
    }

    /// <summary>
    /// AES解密
    /// 密码:自定义
    /// 偏移量:自定义
    /// 模式:CBC
    /// 填充:PKCS7
    /// 编码:UTF8
    /// </summary>
    /// <param name="plaintext">密文</param>
    /// <param name="key">密码,8位,不含中文</param>
    /// <param name="iv">偏移量,16位,不含中文</param>
    /// <returns></returns>
    public static string Decrypt(string plaintext, string key, string iv)
    {
        return Decrypt(plaintext, key, iv, OutputMethod.Base64, CipherMode.CBC, PaddingMode.PKCS7);
    }

    /// <summary>
    /// AES解密
    /// 密码:自定义
    /// 偏移量:自定义
    /// 模式:自定义
    /// 填充:自定义
    /// 编码:UTF8
    /// </summary>
    /// <param name="ciphertext">密文</param>
    /// <param name="key">密码,8位,不含中文</param>
    /// <param name="iv">偏移量,16位,不含中文</param>
    /// <param name="cipher">加密模式</param>
    /// <param name="padding">填充</param>
    /// <returns></returns>
    public static string Decrypt(string ciphertext, string key, string iv, OutputMethod method, CipherMode cipher, PaddingMode padding)
    {
        if (String.IsNullOrEmpty(ciphertext))
        {
            throw new ArgumentNullException("密文不能为空");
        }

        if (String.IsNullOrEmpty(key) || key.Length != 16)
        {
            throw new ArgumentNullException("密钥参数错误");
        }

        if (String.IsNullOrEmpty(iv) || key.Length != 16)
        {
            throw new ArgumentNullException("向量参数错误");
        }

        byte[] buffer = EadUtil.Input(ciphertext, method);

        Rijndael aes = Rijndael.Create();
        aes.Key = Encoding.ASCII.GetBytes(key);
        aes.IV = Encoding.ASCII.GetBytes(iv);
        aes.Mode = cipher;
        aes.Padding = padding;

        using (MemoryStream ms = new MemoryStream())
        {
            CryptoStream cStream = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write);
            cStream.Write(buffer, 0, buffer.Length);
            cStream.FlushFinalBlock();
            ms.Close();
            return Encoding.UTF8.GetString(ms.ToArray());
        }
    }
}

点击查看 OutputMethod、EadUtil

以上是关于加密算法AES的主要内容,如果未能解决你的问题,请参考以下文章

正确使用AES对称加密

正确使用AES对称加密

php与java通用AES加密解密算法

DES加密解密与AES加密解密

对称加密及AES加密算法

加密算法之AES