【加密算法】3DES

一、简介

3DES(或称为Triple DES)是三重数据加密算法(TDEA,Triple Data Encryption Algorithm)块密码的通称。它相当于是对每个数据块应用三次DES加密算法。由于计算机运算能力的增强,原版DES密码的密钥长度变得容易被暴力破解;3DES即是设计用来提供一种相对简单的方法,即通过增加DES的密钥长度来避免类似的攻击,而不是设计一种全新的块密码算法。

二、实现

/// <summary>
/// 3DES加密
/// ECB模式,偏移向量不起作用
/// 当前代码的密钥、偏移向量不可以含有中文
/// 如需跟java、php通用加解密,参数需一致
/// </summary>
public class Des3Helper
{
    private static readonly string _Key = "3dSXKs1oBSzbG@t!AIo#D5cx"; //默认密钥长度可为16位或者24位
    private static readonly string _Vector = "Q9Fvi$SO";  //默认偏移向量可为8位 

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

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

    /// <summary>
    /// 3DES加密
    /// 密码:自定义
    /// 偏移量:自定义
    /// 模式:CBC
    /// 填充:PKCS7
    /// 输出:Base64
    /// 编码:UTF8
    /// </summary>
    /// <param name="plaintext">明文</param>
    /// <param name="key">密钥,16或者24位</param>
    /// <param name="iv">偏移向量,8位</param>
    /// <param name="method">输出编码方式</param>
    /// <param name="cipher">运算模式</param>
    /// <param name="padding">填充模式</param>
    /// <returns>大写的密文</returns>
    public static string Encrypt(string plaintext, string key, string iv, OutputMethod method = OutputMethod.Base64, CipherMode cipher = CipherMode.CBC, PaddingMode padding = PaddingMode.PKCS7)
    {
        if (String.IsNullOrEmpty(plaintext))
        {
            throw new ArgumentNullException("明文不能为空");
        }

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

        TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
        provider.Key = Encoding.UTF8.GetBytes(key);
        provider.IV = Encoding.UTF8.GetBytes(iv);
        provider.Mode = cipher;
        provider.Padding = padding;

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

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



    /// <summary>
    /// 3DES解密
    /// 密码:默认
    /// 偏移量:默认
    /// 模式:CBC
    /// 填充:PKCS7
    /// 内容格式:Base64
    /// 编码:UTF8
    /// </summary>
    /// <param name="ciphertext">密文</param>
    /// <returns>返回明文</returns>
    public static string Decrypt(string ciphertext)
    {
        return Decrypt(ciphertext, _Key, _Vector);
    }

    /// <summary>
    /// 3DES解密
    /// 密码:默认
    /// 偏移量:默认
    /// 模式:CBC
    /// 填充:PKCS7
    /// 内容格式:Base64
    /// 编码:UTF8
    /// </summary>
    /// <param name="ciphertext">密文</param>
    /// <returns>返回明文</returns>
    public static string Decrypt(string ciphertext, OutputMethod method)
    {
        return Decrypt(ciphertext, _Key, _Vector, method);
    }


    /// <summary>
    /// 3DES解密
    /// 密码:自定义
    /// 偏移量:自定义
    /// 模式:CBC
    /// 填充:PKCS7
    /// 内容格式:Base64
    /// 编码:UTF8
    /// </summary>
    /// <param name="ciphertext"></param>
    /// <param name="key"></param>
    /// <param name="iv"></param>
    /// <param name="cipher"></param>
    /// <param name="padding"></param>
    /// <returns></returns>
    public static string Decrypt(string ciphertext, string key, string iv, OutputMethod method = OutputMethod.Base64, CipherMode cipher = CipherMode.CBC, PaddingMode padding = PaddingMode.PKCS7)
    {
        if (String.IsNullOrEmpty(ciphertext))
        {
            throw new ArgumentNullException("密文不能为空");
        }

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

        TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
        provider.Key = Encoding.UTF8.GetBytes(key);
        provider.IV = Encoding.UTF8.GetBytes(iv);
        provider.Mode = cipher;
        provider.Padding = padding;

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

点击查看 OutputMethod、EadUtil

posted @ 2018-12-11 16:20  a boy  阅读(3482)  评论(0编辑  收藏  举报