zoukankan      html  css  js  c++  java
  • 3DES加解密类

    using System;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;
    
    namespace GT.Common.DES
    {
        /// <summary>
        /// 3DES加解密类
        /// </summary>
        public class DESHelper
        {
           
            //构造一个对称算法
            private SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
            #region 加密解密函数
            /// <summary>
            /// 字符串的加密
            /// </summary>
            /// <param name="Value">要加密的字符串</param>
            /// <param name="sKey">密钥,必须32位</param>
            /// <param name="sIV">向量,必须是8个字符</param>
            /// <returns>加密后的字符串</returns>
            public string EncryptString(string Value, string sKey,string sIV)
            {
                try
                {
                    if (string.IsNullOrWhiteSpace(sKey))
                    {
                        sKey = key;
                    }
                    if (string.IsNullOrWhiteSpace(sIV))
                    {
                        sIV = iv;
                    }
                    ICryptoTransform ct;
                    MemoryStream ms;
                    CryptoStream cs;
                    byte[] byt;
                    mCSP.Key = Convert.FromBase64String(sKey);
                    byte[] biv=Encoding.Default.GetBytes (sIV);
    
                    mCSP.IV = biv;
                    //指定加密的运算模式
                    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)
                {
                    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
                {
                    if (string.IsNullOrWhiteSpace(sKey))
                    {
                        sKey = key;
                    }
                    if (string.IsNullOrWhiteSpace(sIV))
                    {
                        sIV = iv;
                    }
                    ICryptoTransform ct;//加密转换运算
                    MemoryStream ms;//内存流
                    CryptoStream cs;//数据流连接到数据加密转换的流
                    byte[] byt;
                   
                    //将3DES的密钥转换成byte
                    mCSP.Key = Convert.FromBase64String(sKey);
                    //将3DES的向量转换成byte
                    mCSP.IV = Encoding.Default.GetBytes(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)
                {
                    return ("Error in Decrypting " + ex.Message);
                }
            }
            #endregion
    
            #region KEY
    
             //<!--不要轻易修改,一定要保证跟生产环境的一致,防止丢失-->
             //<!--秘钥32位-->
             public static readonly string key = "aybcwdefgh1ntv2x3qr4u5o6igklszmp";
             //<!--向量8位-->
             public static readonly string iv = "85632147";
    
             #endregion
    
        }
    
    }
    萌橙 你瞅啥?
  • 相关阅读:
    51nod——1174 区间中最大的数(ST)
    「学习笔记」Ubuntu 下手动安装 Emacs
    [学习笔记] 2-SAT
    「解题报告」 [JXOI2017]数列 (DP)
    「解题报告」 [UOJ#62] 怎样跑得更快 (莫比乌斯反演)
    「学习笔记」杜教筛
    「学习笔记」Dirichlet卷积 莫比乌斯函数 莫比乌斯反演
    「学习笔记」向量外积(叉乘)
    网络流24题
    「解题报告」[网络流24题] 16.数字梯形问题 (最大费用最大流)
  • 原文地址:https://www.cnblogs.com/daimaxuejia/p/11446154.html
Copyright © 2011-2022 走看看