2 wbc3112762 wbc3112762 于 2017.09.14 00:26 提问

求大神写一个UnEncode

这里是一个加密的方法,求大神写一个解密方法。
public static string Encode(string pSource, string pKey)
{
bool flag;
string s2;

        StringBuilder stringBuilder = new StringBuilder();
        pKey = pKey.PadRight(8, '8');
        DESCryptoServiceProvider descryptoServiceProvider = new DESCryptoServiceProvider();
        try
        {
            byte[] bArr1 = Encoding.ASCII.GetBytes(pKey);
            byte[] bArr2 = Encoding.ASCII.GetBytes(pKey);
            byte[] bArr3 = Encoding.UTF8.GetBytes(pSource);
            descryptoServiceProvider.Mode = CipherMode.CBC;
            descryptoServiceProvider.Key = bArr1;
            descryptoServiceProvider.IV = bArr2;
            string s1 = "";
            MemoryStream memoryStream = new MemoryStream();
            try
            {
                CryptoStream cryptoStream = new CryptoStream(memoryStream, descryptoServiceProvider.CreateEncryptor(), CryptoStreamMode.Write);
                try
                {
                    cryptoStream.Write(bArr3, 0, bArr3.Length);
                    cryptoStream.FlushFinalBlock();
                    s1 = Convert.ToBase64String(memoryStream.ToArray());
                }
                finally
                {
                    flag = cryptoStream == null;
                    if (!flag)
                        cryptoStream.Dispose();
                }
            }
            finally
            {
                flag = memoryStream == null;
                if (!flag)
                    memoryStream.Dispose();
            }
            s2 = s1;
        }
        finally
        {
            flag = descryptoServiceProvider == null;
            if (!flag)
                descryptoServiceProvider.Dispose();
        }
        return s2;
    }

3个回答

showbo
showbo   Ds   Rxr 2017.09.14 09:51
已采纳

des加密的吧,用下面的试试了

  /// <summary>
    /// DEC 解密过程
    /// </summary>
    /// <param name="pToDecrypt">被解密的字符串</param>
    /// <returns>返回被解密的字符串</returns>
    public static string Decrypt(string pToDecrypt)
    {
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                des.Mode = CipherMode.CBC;

        byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
        for (int x = 0; x < pToDecrypt.Length / 2; x++)
        {
            int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
            inputByteArray[x] = (byte)i;
        }
                string deskey = pKey.PadRight(8, '8');
        des.Key = ASCIIEncoding.ASCII.GetBytes(deskey); //建立加密对象的密钥和偏移量,此值重要,不能修改
        des.IV = ASCIIEncoding.ASCII.GetBytes(deskey);
        MemoryStream ms = new MemoryStream();
        CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);

        cs.Write(inputByteArray, 0, inputByteArray.Length);
        cs.FlushFinalBlock();

        //建立StringBuild对象,createDecrypt使用的是流对象,必须把解密后的文本变成流对象
        StringBuilder ret = new StringBuilder();

        return System.Text.Encoding.UTF8.GetString(ms.ToArray());
    }

之前这个对应的加密是这样的,和你的有点出入,不懂能否解密



    /// <summary>
    /// DEC 加密过程
    /// </summary>
    /// <param name="pToDecrypt">被解密的字符串</param>
    /// <returns>返回被解密的字符串</returns>
    public static string Encrypt(string pToEncrypt)
    {
                string deskey = pKey.PadRight(8, '8');

        //访问数据加密标准(DES)算法的加密服务提供程序 (CSP) 版本的包装对象
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        des.Key = ASCIIEncoding.ASCII.GetBytes(deskey); //建立加密对象的密钥和偏移量
        des.IV = ASCIIEncoding.ASCII.GetBytes(deskey);  //原文使用ASCIIEncoding.ASCII方法的GetBytes方法

        byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);//把字符串放到byte数组中

        MemoryStream ms = new MemoryStream();//创建其支持存储区为内存的流 
        //定义将数据流链接到加密转换的流
        CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
        cs.Write(inputByteArray, 0, inputByteArray.Length);
        cs.FlushFinalBlock();
        //上面已经完成了把加密后的结果放到内存中去

        StringBuilder ret = new StringBuilder();
        foreach (byte b in ms.ToArray())
        {
            ret.AppendFormat("{0:X2}", b);
        }
        ret.ToString();
        return ret.ToString();
    }
devmiao
devmiao   Ds   Rxr 2017.09.14 08:43

这个你可以根据算法自己写下。

wbc3112762
wbc3112762   2017.09.15 01:18

///
/// DEC 解密过程
///
/// 被解密的字符串
/// 返回被解密的字符串
public static string Decrypt(string pToDecrypt, string pKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Mode = CipherMode.CBC;

        byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);

        string deskey = pKey.PadRight(8, '8');
        des.Key = ASCIIEncoding.ASCII.GetBytes(deskey); //建立加密对象的密钥和偏移量,此值重要,不能修改
        des.IV = ASCIIEncoding.ASCII.GetBytes(deskey);
        MemoryStream ms = new MemoryStream();
        CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);

        cs.Write(inputByteArray, 0, inputByteArray.Length);
        cs.FlushFinalBlock();

        //建立StringBuild对象,createDecrypt使用的是流对象,必须把解密后的文本变成流对象
        StringBuilder ret = new StringBuilder();

        return System.Text.Encoding.UTF8.GetString(ms.ToArray());
    }
Csdn user default icon
上传中...
上传图片
插入图片