【加密算法】3DES

1、簡介php

3DES(或稱爲Triple DES)是三重數據加密算法(TDEA,Triple Data Encryption Algorithm)塊密碼的通稱。它至關因而對每一個數據塊應用三次DES加密算法。因爲計算機運算能力的加強,原版DES密碼的密鑰長度變得容易被暴力破解;3DES便是設計用來提供一種相對簡單的方法,即經過增長DES的密鑰長度來避免相似的攻擊,而不是設計一種全新的塊密碼算法。html

2、實現java

/// <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算法

相關文章
相關標籤/搜索