【加密算法】DES

一、简介

DES对称加密,是一种比较传统的加密方式,其加密运算、解密运算使用的是同样的密钥,信息的发送者和信息的接收者在进行信息的传输与处理时,必须共同持有该密码(称为对称密码),是一种对称加密算法。

DES是一个分组加密算法,典型的DES以64位为分组对数据加密,加密和解密用的是同一个算法。该算法把64位密码中的第8位、第16位、第24位、第32位、第40位、第48位、第56位、第64位作为奇偶校验位,在计算密钥时要忽略这8位.如果输入的密码只是在这8位上有区别的话,那么操作后的结果将是一样的. 

二、实现

/// <summary>
/// DES加密解密类
/// ECB模式,偏移向量不起作用
/// 当前代码的密钥、偏移向量不可以含有中文
/// 如需跟java、php通用加解密,参数需一致
/// </summary>
public class DesHelper
{
    private static readonly string _Key = "!@#Swg68";   //默认密钥 长度需8位
    private static readonly string _Vector = "vb13$@FG"; //默认偏移向量 长度需8位

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

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

    /// <summary>
    /// DES加密
    /// 密码:自定义
    /// 偏移量:自定义
    /// 模式:CBC
    /// 填充:PKCS7
    /// 输出:Base64
    /// 编码:UTF8
    /// </summary>
    /// <param name="plaintext">明文</param>
    /// <param name="key">密码,8位,不含中文</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("明文不能为空");
        }

        Encoding encoding = Encoding.UTF8;
        byte[] byteContent = encoding.GetBytes(plaintext);
        byte[] byteKey = encoding.GetBytes(key);
        byte[] byteIV = encoding.GetBytes(iv);

        DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
        provider.Key = byteKey;
        provider.IV = byteIV;
        provider.Mode = cipher;
        provider.Padding = padding;

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

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




    /// <summary>
    /// DES解密   输出编码必须跟加密的输出编码一致
    /// 密码:默认
    /// 偏移量:默认
    /// 模式:CBC
    /// 填充:PKCS7
    /// 编码:UTF8
    /// </summary>
    /// <param name="ciphertext">密文</param>
    /// <returns>返回明文</returns>
    public static string Decrypt(string ciphertext)
    {
        return Decrypt(ciphertext, _Key, _Vector);
    }

    /// <summary>
    /// DES解密   输出编码必须跟加密的输出编码一致
    /// 密码:默认
    /// 偏移量:默认
    /// 模式:CBC
    /// 填充:PKCS7
    /// 编码:UTF8
    /// </summary>
    /// <param name="ciphertext">密文</param>
    /// <returns>返回明文</returns>
    public static string Decrypt(string ciphertext, OutputMethod method)
    {
        return Decrypt(ciphertext, _Key, _Vector, method);
    }


    /// <summary>
    /// DES解密 输出编码必须跟加密的输出编码一致
    /// 密码:自定义
    /// 偏移量:自定义
    /// 模式:CBC
    /// 填充:PKCS7
    /// 编码:UTF8
    /// </summary>
    /// <param name="ciphertext">密文</param>
    /// <param name="key">密码,8位,不含中文</param>
    /// <param name="iv">偏移量,8位,不含中文</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);

        Encoding encoding = Encoding.UTF8;
        byte[] byteKey = encoding.GetBytes(key);
        byte[] byteIV = encoding.GetBytes(iv);

        DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
        provider.Key = byteKey;
        provider.IV = byteIV;
        provider.Mode = cipher;
        provider.Padding = padding;

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

点击查看 OutputMethod、EadUtil

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