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

        /// <summary>
        ///3DES加密
        /// </summary>
        /// <param name="originalValue">加密数据</param>
        /// <param name="key">24位字符的密钥字符串</param>
        /// <param name="IV">8位字符的初始化向量字符串</param>
        /// <returns></returns>
        public string DESEncrypt(string originalValue, string key, string IV)
        {
    
            SymmetricAlgorithm sa;
            ICryptoTransform ct;
            MemoryStream ms;
            CryptoStream cs;
            byte[] byt;
            sa = new TripleDESCryptoServiceProvider();
            sa.Key = Encoding.UTF8.GetBytes(key);
            sa.IV = Encoding.UTF8.GetBytes(IV);
            ct = sa.CreateEncryptor();
            byt = Encoding.UTF8.GetBytes(originalValue);
            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());
        }
         /// <summary>
        /// 3DES解密
         /// </summary>
        /// <param name="data">解密数据</param>
        /// <param name="key">24位字符的密钥字符串(需要和加密时相同)</param>
        /// <param name="iv">8位字符的初始化向量字符串(需要和加密时相同)</param>
        /// <returns></returns>
        public static string DESDecrypst(string data, string key, string IV)
        {
            SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider() ;
            mCSP.Key = Encoding.UTF8.GetBytes(key);
            mCSP.IV = Encoding.UTF8.GetBytes(IV);
            ICryptoTransform ct;
            MemoryStream ms;
            CryptoStream cs;
            byte[] byt;
            ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
            byt = Convert.FromBase64String(data);
            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());
    
        }


    调用方法:

    String desKey = "1dcrm4goRY8KODsgV1PPuHLB";//24位密钥
    String desIv = "QCsJ2SKR"; ///8位向量

    var request = DESEncrypt("123456789", desKey, desIv);//加密

    var request = DESDecrypt("123456789", desKey, desIv);//解密



  • 相关阅读:
    洛谷P3455
    开发人员的奋斗目标
    js判读周末以及节假日
    c#中集成Swagger
    Combo Select – jQuery可搜索下拉框插件
    接口对接 调用与处理方式
    问题集锦
    sql server 自定义函数的使用
    Api接口服务的设计和安全解决方案
    使用Jquery Ajax请求 下载压缩文件
  • 原文地址:https://www.cnblogs.com/yuanye0918/p/5740734.html
Copyright © 2011-2022 走看看