js在页面用des加密,密文到后台用c#的des解密程序进行解密,请教高人?  

解决方案 »

  1.   

    /// <summary>
    /// DES解密,sKey可为8位或16位,默认请指定为:"8lvbe4kE"
    /// </summary>
    /// <param name="pToDecrypt">需要解密的字符串</param>
    /// <param name="sKey">密钥</param>
    /// <returns>解密后的字符串</returns>
    public static string DESDecrypt(string pToDecrypt, string sKey)
    {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();        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;
            }        des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();        StringBuilder ret = new StringBuilder();        return System.Text.Encoding.Default.GetString(ms.ToArray());
    }