C#的3DES加密解密算法实例代码

Xandy ·
更新时间:2024-11-10
· 658 次阅读

C#类如下:
代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace ConsoleApplication1
{
    /// <summary>
    /// 加解密类
    /// </summary>
    public class EncryptHelper
    {
       

        //构造一个对称算法
        private SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();

        #region 加密解密函数

        /// <summary>
        /// 字符串的加密
        /// </summary>
        /// <param name="Value">要加密的字符串</param>
        /// <param name="sKey">密钥,必须32位</param>
        /// <param name="sIV">向量,必须是12个字符</param>
        /// <returns>加密后的字符串</returns>
        public string EncryptString(string Value, string sKey,string sIV)
        {
            try
            {
                ICryptoTransform ct;
                MemoryStream ms;
                CryptoStream cs;
                byte[] byt;
                mCSP.Key = Convert.FromBase64String(sKey);
                mCSP.IV = Convert.FromBase64String(sIV);
                //指定加密的运算模式
                mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
                //获取或设置加密算法的填充模式
                mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
                ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);//创建加密对象
                byt = Encoding.UTF8.GetBytes(Value);
                ms = new MemoryStream();
                cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
                cs.Write(byt, 0, byt.Length);
                cs.FlushFinalBlock();
                cs.Close();

                return Convert.ToBase64String(ms.ToArray());
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message, "出现异常", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return ("Error in Encrypting " + ex.Message);
            }
        }

        /// <summary>
        /// 解密字符串
        /// </summary>
        /// <param name="Value">加密后的字符串</param>
        /// <param name="sKey">密钥,必须32位</param>
        /// <param name="sIV">向量,必须是12个字符</param>
        /// <returns>解密后的字符串</returns>
        public string DecryptString(string Value, string sKey, string sIV)
        {
            try
            {
                ICryptoTransform ct;//加密转换运算
                MemoryStream ms;//内存流
                CryptoStream cs;//数据流连接到数据加密转换的流
                byte[] byt;
                //将3DES的密钥转换成byte
                mCSP.Key = Convert.FromBase64String(sKey);
                //将3DES的向量转换成byte
                mCSP.IV = Convert.FromBase64String(sIV);
                mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
                mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
                ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);//创建对称解密对象
                byt = Convert.FromBase64String(Value);
                ms = new MemoryStream();
                cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
                cs.Write(byt, 0, byt.Length);
                cs.FlushFinalBlock();
                cs.Close();

                return Encoding.UTF8.GetString(ms.ToArray());
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message, "出现异常", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return ("Error in Decrypting " + ex.Message);
            }
        }

        #endregion
    }
}

调用方法如下:
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            EncryptHelper helper = new EncryptHelper();

            //加密
            string oldValue = "13800138000";
            //加密后结果
            //密钥,必须32位
            string sKey = "qJzGEh6hESZDVJeCnFPGuxzaiB7NLQM5";
            //向量,必须是12个字符
            string sIV = "andyliu1234=";
            //print
            string newValue =  helper.EncryptString(oldValue,sKey,sIV);
            Console.WriteLine("加密后:"+ newValue);
            //解密
            string desValue = helper.DecryptString(newValue,sKey,sIV);
            //
            Console.WriteLine("解密后:"+ desValue);
            Console.ReadLine();

        }
    }
}

您可能感兴趣的文章:C#实现对AES加密和解密的方法C#实现的AES加密解密完整实例c#多种加解密示例(md5加密解密)基于C#对用户密码使用MD5加密与解密C#编写的Base64加密和解密类C#编写DES加密、解密类C#自定义RSA加密解密及RSA签名和验证类实例C#实现对文件进行加密解密的方法C#最简单的字符串加密解密方法C#开发中经常用的加密解密方法示例



C# des加密 des 算法

需要 登录 后方可回复, 如果你还没有账号请 注册新账号