#region   DES 加密
        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="pToEncrypt">需加密的字符串</param>
        /// <param name="sKey">密钥(8位英文字符)</param>
        /// <returns></returns>
        public static string DESEncrypt(string pToEncrypt, string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            //把字符串放到byte数组中
            //原来使用的UTF8编码,我改成Unicode编码了,不行
            byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);            //建立加密对象的密钥和偏移量
            //使得输入密码必须输入英文文本
            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            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();
        }
        #endregion        #region  DES 解密
        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="pToDecrypt">需要解密的字符串</param>
        /// <param name="sKey">密钥(8位英文字符)</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();
            //建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象
            StringBuilder ret = new StringBuilder();
            return System.Text.Encoding.Default.GetString(ms.ToArray());
        }        #endregion上面一段是DES加密和解密的方法。源于网络。。
调用示例:
Response.Redirect("WebForm1.aspx?id="+EncryOperate.DESEncrypt("这是文字","abcdefgh"));解密却是问号???注:在英文服务器出现这样的问题,中文服务器没问题。
求解

解决方案 »

  1.   

      return System.Text.Encoding.Default.GetString(ms.ToArray());将编码设成 GB2312也是不行
      

  2.   

    Encoding.Default不是Unicode,而且不同机器可以出现不同 - 它取决于系统当前的代码页设定。
      

  3.   

    我将你的代码在我的系统(win7 企业英文版 )上跑了一下解密没有问题
      

  4.   


    Encoding.Unicode 我试了试 也是不行。。
    显示的是一堆诡异的文字
      

  5.   

            public static string DESEncrypt(string pToEncrypt, string sKey)
            {
                byte[] inputByteArray = Encoding.Unicode.GetBytes(pToEncrypt);
            }
            public static string DESDecrypt(string pToDecrypt, string sKey)
            {
                return Encoding.Unicode.GetString(ms.ToArray());
            }
      

  6.   

    我的是 win2003 EN 不行。
    不知道 胃神马
      

  7.   


    void Main()
    {
    string s=DESEncrypt("我是文字","abcdefgh");
       Console.WriteLine(s);
       Console.WriteLine(DESDecrypt(s,"abcdefgh"));
    }#region   DES 加密
    /// <summary>
    /// DES加密
    /// </summary>
    /// <param name="pToEncrypt">需加密的字符串</param>
    /// <param name="sKey">密钥(8位英文字符)</param>
    /// <returns></returns>
    public static string DESEncrypt(string pToEncrypt, string sKey)
    {
    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    //把字符串放到byte数组中
    //原来使用的UTF8编码,我改成Unicode编码了,不行
    byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt); //建立加密对象的密钥和偏移量
    //使得输入密码必须输入英文文本
    des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
    des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
    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();
    }
    #endregion #region  DES 解密
    /// <summary>
    /// DES解密
    /// </summary>
    /// <param name="pToDecrypt">需要解密的字符串</param>
    /// <param name="sKey">密钥(8位英文字符)</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();
    //建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象
    StringBuilder ret = new StringBuilder();
    return System.Text.Encoding.Default.GetString(ms.ToArray());
    } #endregion/*
    结果:
    8F3E933BEF3A2A3368204441D1154BF9
    我是文字
    /*
      

  8.   


     DES 解密
    改成这个看看
    return System.Text.Encoding.Default.GetString(ms.ToArray());
    ====>
    return   System.Text.Encoding.UTF8.GetString(ms.ToArray());