博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 3DES 加密算法
阅读量:4959 次
发布时间:2019-06-12

本文共 2358 字,大约阅读时间需要 7 分钟。

using System;

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

namespace ChalwinIssue

{
    /// <summary>
    /// 加解密类
    /// </summary>
    public class EdOperator
    {
        //密钥
        private const string sKey = "qJzGEh6hESZDVJeCnFPGuxzaiB7NLQM3";
        //向量,必须是12个字符
        private const string sIV = "andyliu1234=";

        //构造一个对称算法

        private SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();

        #region 加密解密函数

        /// <summary>

        /// 字符串的加密
        /// </summary>
        /// <param name="Value">要加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public string EncryptString(string Value)
        {
            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>
        /// <returns>解密后的字符串</returns>
        public string DecryptString(string Value)
        {
            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

    }
}

转载于:https://www.cnblogs.com/liuxchen/archive/2012/07/15/2592766.html

你可能感兴趣的文章
Gold Smith第一章
查看>>
生成php所需要的APNS Service pem证书的步骤
查看>>
JavaWeb之JSON
查看>>
URL中的特殊字符处理
查看>>
HOT SUMMER 每天都是不一样,积极的去感受生活 C#关闭IE相应的窗口 .
查看>>
windows平台上编译mongdb-cxx-driver
查看>>
optionMenu-普通菜单使用
查看>>
MVC3分页传2参
查看>>
2016-2017-2点集拓扑作业[本科生上课时]讲解视频
查看>>
appium(13)- server config
查看>>
IIS负载均衡-Application Request Route详解第六篇:使用失败请求跟踪规则来诊断ARR...
查看>>
管理信息系统 第三部分 作业
查看>>
[Leetcode Week13]Search a 2D Matrix
查看>>
查看端口占用cmd命令
查看>>
2019.01.17王苛震作业
查看>>
Halcon学习(八)文本操作
查看>>
MFC电子词典
查看>>
简单工厂(Simple Factory)
查看>>
04: 打开tornado源码剖析处理过程
查看>>
02: 安装epel 解决centos7无法使用yum安装nginx
查看>>