当前位置:  编程技术>.net/c#/asp.net

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

    来源: 互联网  发布时间:2014-10-24

    本文导语:  C#类如下: 代码如下:using System;using System.Collections.Generic;using System.Text;using System.Security.Cryptography;using System.IO; namespace ConsoleApplication1{    ///     /// 加解密类    ///     public class EncryptHelper    {                //构...

C#类如下:

代码如下:

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

namespace ConsoleApplication1
{
    ///
    /// 加解密类
    ///
    public class EncryptHelper
    {
       

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

        #region 加密解密函数

        ///
        /// 字符串的加密
        ///
        /// 要加密的字符串
        /// 密钥,必须32位
        /// 向量,必须是12个字符
        /// 加密后的字符串
        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);
            }
        }

        ///
        /// 解密字符串
        ///
        /// 加密后的字符串
        /// 密钥,必须32位
        /// 向量,必须是12个字符
        /// 解密后的字符串
        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();

        }
    }
}


    
 
 
 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • 有熟悉3Des算法的人吗?
  • 跪求3DES ECB 模式的加密C源码
  • 字符串desede 3des加密示例分享
  • 一个可逆加密的类(使用3DES加密)
  • 6个0和6个1作为密钥用3des加密结果一样,请帮我看下这个程序的问题


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3