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);
        }

    }
}
原文地址:https://www.cnblogs.com/myLeisureTime/p/14655090.html