当前位置: 首页 > 工具软件 > EDE > 使用案例 >

3DES(DESede、Triple-DES、DES-EDE) 加解密 C#

左丘嘉言
2023-12-01

对称加密(DES、3DES)、非对称加密(RSA、SHA1withRSA)
参考:
https://blog.csdn.net/sinat_16998945/article/details/94639023

3DES、DESede、Triple-DES、DES-EDE名称相互都是别名

一、C# 加解密代码

默认模式为:DESede/ECB/PKCS7

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Common.Utils
{

    /// <summary>
    /// DESede加解密
    /// </summary>
    public class DESedeUtils
    {

        /// <summary>
        /// //指定加密的运算模式
        /// </summary>
        private CipherMode cipherMode = CipherMode.ECB;

        /// <summary>
        ///  指定加密的填充模式
        /// </summary>
        private PaddingMode paddingMode = PaddingMode.PKCS7;

        /// <summary>
        ///  指定对称算法
        /// </summary>
        private SymmetricAlgorithm getSymmetricAlgorithm(string key, string iv = "") {
            SymmetricAlgorithm symmetricAlgorithm = new TripleDESCryptoServiceProvider();
            symmetricAlgorithm.Key = Convert.FromBase64String(key);
            symmetricAlgorithm.Mode = cipherMode;
            symmetricAlgorithm.Padding = paddingMode;
            if (!string.IsNullOrWhiteSpace(iv))
            {
                symmetricAlgorithm.IV = Convert.FromBase64String(iv);
            }

            return symmetricAlgorithm;
        }


        /// <summary>
        /// 字符串的加密
        /// 模式:ECB/PKCS7
        /// </summary>
        /// <param name="encryptStr">待加密的字符串</param>
        /// <param name="key">密钥,必须32位</param>
        /// <param name="iv">向量,必须是12个字符</param>
        /// <returns>加密后的字符串</returns>
        public string Encrypt(string encryptStr, string key, string iv = "")
        {
            try
            {
                SymmetricAlgorithm symmetricAlgorithm = this.getSymmetricAlgorithm(key,iv);
                ICryptoTransform ct = symmetricAlgorithm.CreateEncryptor(symmetricAlgorithm.Key, symmetricAlgorithm.IV);

                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
                byte[] byt = Encoding.UTF8.GetBytes(encryptStr);
                cs.Write(byt, 0, byt.Length);
                cs.FlushFinalBlock();
                cs.Close();

                return Convert.ToBase64String(ms.ToArray());
            }
            catch (Exception ex)
            {
                return ("异常:" + ex.Message);
            }
        }

        /// <summary>
        /// 解密字符串
        /// 模式:ECB/PKCS7
        /// </summary>
        /// <param name="decryptStr">加密后的字符串</param>
        /// <param name="sKey">密钥,必须32位</param>
        /// <param name="sIV">向量,必须是12个字符</param>
        /// <returns>解密后的字符串</returns>
        public string Decrypt(string decryptStr, string key, string iv = "")
        {
            try
            {
                SymmetricAlgorithm symmetricAlgorithm = this.getSymmetricAlgorithm(key, iv);
                ICryptoTransform ct = symmetricAlgorithm.CreateDecryptor(symmetricAlgorithm.Key, symmetricAlgorithm.IV);

                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
                byte[] byt = Convert.FromBase64String(decryptStr);
                cs.Write(byt, 0, byt.Length);
                cs.FlushFinalBlock();
                cs.Close();

                return Encoding.UTF8.GetString(ms.ToArray());
            }
            catch (Exception ex)
            {
                return ("异常:" + ex.Message);
            }
        }
    }
}

二、加解密调用方法

string encryptStr = "001张三*", key = "5d48b92bfcccc1db9d3d965b32ead033";

DESedeUtils _DESedeUtils = new DESedeUtils();
//加密
string encrypt = _DESedeUtils.Encrypt(encryptStr, key);
//加密结果 encrypt = eAgalz8MjYceOOI5V77fgA==

//解密
string decryptStr = _DESedeUtils.Decrypt(encrypt, key);
//解密结果 decryptStr = 001张三*
 类似资料: