加密算法DES

Posted weiweixiang

tags:

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

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

/// <summary>
/// DES加密解密类
///
/// </summary>
public class DesHelper
{
    private static readonly string _Key = "[email protected]#Swg68";
    private static readonly string _Vector = "[email protected]";

    /// <summary>
    /// DES加密
    /// 密码:默认
    /// 偏移量:默认
    /// 模式: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>
    /// 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, CipherMode.CBC, PaddingMode.PKCS7);
    }

    /// <summary>
    /// DES加密
    /// 密码:自定义
    /// 偏移量:自定义
    /// 模式:CBC
    /// 填充:PKCS7
    /// 输出:Base64
    /// 编码:UTF8
    /// </summary>
    /// <param name="plaintext">明文</param>
    /// <param name="key">密码,8位,不含中文</param>
    /// <param name="iv">偏移量,8位,不含中文</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>
    /// DES加密
    /// 密码:自定义
    /// 偏移量:自定义
    /// 输出:自定义
    /// 模式:CBC
    /// 填充:PKCS7
    /// 编码:UTF8
    /// </summary>
    /// <param name="plaintext"></param>
    /// <param name="key"></param>
    /// <param name="iv"></param>
    /// <param name="method"></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>
    /// DES加密
    /// 密码:自定义
    /// 偏移量:自定义
    /// 模式:自定义
    /// 填充:自定义
    /// 输出:自定义
    /// 编码: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, CipherMode cipher, PaddingMode padding)
    {
        if (String.IsNullOrEmpty(plaintext))
        {
            throw new ArgumentNullException("明文不能为空");
        }

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

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

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

        DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
        provider.Key = Encoding.ASCII.GetBytes(key);
        provider.IV = Encoding.ASCII.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>
    /// DES解密   输出编码必须跟加密的输出编码一致
    /// 密码:默认
    /// 偏移量:默认
    /// 模式:CBC
    /// 填充:PKCS7
    /// 编码:UTF8
    /// </summary>
    /// <param name="ciphertext">密文</param>
    /// <returns>返回明文</returns>
    public static string Decrypt(string ciphertext)
    {
        return Decrypt(ciphertext, _Key, _Vector, OutputMethod.Base64, CipherMode.CBC, PaddingMode.PKCS7);
    }

    /// <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, CipherMode.CBC, PaddingMode.PKCS7);
    }

    /// <summary>
    /// DES解密   输出编码必须跟加密的输出编码一致
    /// 密码:自定义
    /// 偏移量:自定义
    /// 模式:CBC
    /// 填充:PKCS7
    /// 编码:UTF8
    /// </summary>
    /// <param name="ciphertext">密文</param>
    /// <param name="key">密码,8位,不含中文</param>
    /// <param name="iv">偏移量,8位,不含中文</param>
    /// <returns>返回明文</returns>
    public static string Decrypt(string ciphertext, string key, string iv)
    {
        return Decrypt(ciphertext, key, iv, OutputMethod.Base64, CipherMode.CBC, PaddingMode.PKCS7);
    }

    /// <summary>
    /// DES解密
    /// </summary>
    /// <param name="ciphertext"></param>
    /// <param name="key"></param>
    /// <param name="iv"></param>
    /// <param name="method"></param>
    /// <returns></returns>
    public static string Decrypt(string ciphertext, string key, string iv, OutputMethod method)
    {
        return Decrypt(ciphertext, key, iv, method, CipherMode.CBC, PaddingMode.PKCS7);
    }

    /// <summary>
    /// DES解密 输出编码必须跟加密的输出编码一致
    /// 密码:自定义
    /// 偏移量:自定义
    /// 模式:自定义
    /// 填充:自定义
    /// 编码: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, CipherMode cipher, PaddingMode padding)
    {
        if (String.IsNullOrEmpty(ciphertext))
        {
            throw new ArgumentNullException("密文不能为空");
        }

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

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

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

        DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
        provider.Key = Encoding.ASCII.GetBytes(key);
        provider.IV = Encoding.ASCII.GetBytes(iv);
        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.GetEncoding("UTF-8").GetString(ms.ToArray());
        }
    }
}

点击查看 OutputMethod、EadUtil

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

DES 加密算法解析

java des 加密 解密 密钥随机取得方法

对称密码——DES加密算法

swift中的DES加密解密算法

用c语言写des加密算法

DES加密算法详细原理以及Java代码实现