/// <summary>
        /// DES解密字符串
        /// </summary>
        /// <param name="decryptString">待解密的字符串</param>
        /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
        /// <returns>解密成功返回解密后的字符串,失败返源串</returns>
        public static string Decrypt(string cryptedString, string decryptKey)
        {
            byte[] bytes = ASCIIEncoding.ASCII.GetBytes(decryptKey);
            string s1 = string.Empty;
            if (String.IsNullOrEmpty(cryptedString))
            {
                throw new ArgumentNullException("The string which needs to be decrypted can not be null.");
            }            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
            cryptoProvider.Mode = CipherMode.ECB;
            cryptoProvider.Padding = PaddingMode.Zeros;            //  通过Base64解密出16进制
            s1 = Encoding.Default.GetString(Convert.FromBase64String(cryptedString));            //  16进制数据在转成字符串
            //cryptedString = DivideString(s1,2);            //  16进制数据转BYTE
            Byte[] bBuffer = Hex2ByteArr(s1);            //  开始解密
            MemoryStream memoryStream = new MemoryStream(bBuffer);
            CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read);
            StreamReader reader = new StreamReader(cryptoStream);            return reader.ReadToEnd();
        }以上代码解密的时候容易出错。
执行到以下代码的时候就报错。
//  通过Base64解密出16进制
s1 = Encoding.Default.GetString(Convert.FromBase64String(cryptedString));
报错错误是:Base-64 字符串中的无效字符。请高手指教。多谢!

解决方案 »

  1.   

    好简单吗,检查出现无效字符的地方,看看是不是错误的,是不是密文不对,是不是转换中加入了错误信息,截断不对,之类的。或者结尾不对,去仔细研究一下Base-64的规则。就知道了
      

  2.   

    你看看你这串base64编码字符串有没有问题。邮件的base64内容是以"base64"开头,“----”结尾(具体忘记是什么多,记得是好多个减号)。从“报错错误是:Base-64 字符串中的无效字符。”来看,应该是base64字符串本身有问题,我同事以前也遇到这样的问题,原来是尾部没有去掉结束字符串(也就是很多减号那串)。
      

  3.   


    /// <summary>
            /// 进行DES加密。
            /// </summary>
            /// <param name="pToEncrypt">要加密的字符串。</param>
            /// <param name="sKey">密钥,且必须为8位。</param>
            /// <returns>以Base64格式返回的加密字符串。</returns>
            public string Encrypt(string pToEncrypt, string sKey)
            {
                using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
                {
                    byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);
                    des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                    des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                    System.IO.MemoryStream ms = new System.IO.MemoryStream();
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(inputByteArray, 0, inputByteArray.Length);
                        cs.FlushFinalBlock();
                        cs.Close();
                    }
                    string str = Convert.ToBase64String(ms.ToArray());
                    ms.Close();
                    return str;
                }
            }        /// <summary>
            /// 进行DES解密。
            /// </summary>
            /// <param name="pToDecrypt">要解密的以Base64</param>
            /// <param name="sKey">密钥,且必须为8位。</param>
            /// <returns>已解密的字符串。</returns>
            public string Decrypt(string pToDecrypt, string sKey)
            {
                byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);
                using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
                {
                    des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                    des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                    System.IO.MemoryStream ms = new System.IO.MemoryStream();
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(inputByteArray, 0, inputByteArray.Length);
                        cs.FlushFinalBlock();
                        cs.Close();
                    }
                    string str = Encoding.UTF8.GetString(ms.ToArray());
                    ms.Close();
                    return str;
                }
            }        
      

  4.   

    参考周公的blog
    http://zhoufoxcn.blog.51cto.com/792419/167095
      

  5.   

    http://www.google.com.hk/search?q=base64%E7%BC%96%E7%A0%81&rls=com.microsoft:zh-cn:IE-SearchBox&ie=UTF-8&oe=UTF-8&sourceid=ie7&hl=zh-CN自己看吧。如果只是你的字符串不符合base64编码的问题,这种问题不值得在csdn上来问。自己用点精力学才能真懂。