zoukankan      html  css  js  c++  java
  • c#解密des

    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.IO;
    using System.Linq;
    using System.Security.Cryptography;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace BzdCMS.Common
    {
        public class CryptoJS
        {
            /// <summary>
            /// 解密字符串
            /// </summary>
            /// <param name="str"></param>
            /// <param name="myKey"></param>
            /// <returns></returns>
            public static string DecryptString(string str, string myKey)
            {
                string encryptKeyall = Convert.ToString(myKey);    //定义密钥  
                if (encryptKeyall.Length < 9)
                {
                    for (; ; )
                    {
                        if (encryptKeyall.Length < 9)
                            encryptKeyall += encryptKeyall;
                        else
                            break;
                    }
                }
                string encryptKey = encryptKeyall.Substring(0, 8);
                DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();   //实例化加/解密类对象   
                descsp.Mode = CipherMode.ECB;
    
                byte[] key = Encoding.UTF8.GetBytes(encryptKey); //定义字节数组,用来存储密钥    
                byte[] data = Convert.FromBase64String(str);//定义字节数组,用来存储要解密的字符串  
                byte[] result;
                using (MemoryStream outStream = new MemoryStream())
                {
                    using (CryptoStream encStream = new CryptoStream(outStream, descsp.CreateDecryptor(key, key), CryptoStreamMode.Write))
                    {
                        encStream.Write(data, 0, data.Length);
                        encStream.Close();
                        result = outStream.ToArray();
                    }
                }
                return Encoding.UTF8.GetString(result);
            }
    
        }
    }
  • 相关阅读:
    【angularJS】启动(bootstrap)机制
    【angularJS】定义模块angular.module
    【angularJS】简介
    .NET AutoMapper学习记录
    WebAPI学习
    【MongoDB】在C#中使用
    RabbitMQ介绍及windows下安装使用
    【MongoDB】初识
    php命名空间(nameSpace)的使用详解
    魔术方法
  • 原文地址:https://www.cnblogs.com/myLeisureTime/p/14655090.html
Copyright © 2011-2022 走看看