输入不是有效的Base64字符串,因为它包含非基数64字符?

时间:2012-12-12 07:57:34

标签: c# base64

这里我试图使用私钥解密字符串并得到如下错误:

  

输入不是有效的Base-64字符串,因为它包含非基本64个字符,两个以上的填充字符或填充字符中的非空白字符。

我想要解决的字符串(即字符串authenticationString)是:

  

Z7W7ja7G + NB3 + QsbNnsz7zCkt1xeZ1PQC606wZzQG2McjExT6WjFPDWNpVSqcw1X + K6TERZUK4677m5Z6x9TuxLxyA8h8LmB4dwcJsQZGoVg0mOLsxO6GZmdThLyQOxQgnA7sk4KHLv6DrVswtVzjM / gJouvDKHTC7 + NZmjhWwA =

我的代码是:

 internal virtual Credentials Extract(string basicAuthenticationCredentials)
      {
         string authenticationString = RemoveBasicFromAuthenticationString(basicAuthenticationCredentials);
         string privateKeyPath = @"D:\Bala\MVC\RestService\RestClient\Scripts\PrivateKey.xml";
         myRsa.LoadPrivateFromXml(privateKeyPath);  // Loading the Private key
         RSACryptoServiceProvider localRsa = new RSACryptoServiceProvider();
         localRsa.FromXmlString(File.ReadAllText(privateKeyPath));
         byte[] decMessage = Convert.FromBase64String(authenticationString);
         byte[] message = null;
         // Calling the right decryption method according to the user selection
         message = myRsa.PrivateDecryption(decMessage);
         string au = Encoding.UTF8.GetString(message);
         return extractor.Extract(decoder.Decode(au));
      }

并在字符串au中我正在设置如下值:

  

d ! u I| 3 iaȴ{ȱ q A z ta i8? - [ # *& Y^升,VA \ F $ RV&安培;克;

并且仅针对特定用户名密码收到此错误。其他人工作正常。 有什么建议吗?

编辑:   错误是抛出这一行byte [] decodingStringInBytes = Convert.FromBase64String(encodedValue);

internal virtual string Decode(string encodedValue)
      {
         byte[] decodedStringInBytes = Convert.FromBase64String(encodedValue);
         return Encoding.ASCII.GetString(decodedStringInBytes);
      }

编辑2:

internal class DecodedCredentialsExtractor
   {
      internal virtual Credentials Extract(string credentials)
      {
         if (!string.IsNullOrEmpty(credentials))
         {
            string[] credentialTokens = credentials.Split(':');
            //string securityToken = string.Empty;
            if (credentialTokens.Length == 2)
            {
               return new Credentials(credentialTokens[0], credentialTokens[1]);
            }
         }

         throw new ArgumentException("The supplied credential string is invalid, it should comply to [username:password]", "credentials");
      }
   }

2 个答案:

答案 0 :(得分:2)

您获得au的值的事实表明它不是错误的问题中的base-64解码。我将假设这实际上是失败的最后一行,并且期望base-64 - 并且你正在使用UTF-8(向后,我怀疑,这是无效的)。尝试:

     message = myRsa.PrivateDecryption(decMessage);
     string au = Convert.ToBase64String(message);
     return extractor.Extract(decoder.Decode(au));

答案 1 :(得分:-3)

卸载64位c ++重新分发并安装x84,你应该没有问题。 祝你好运!

相关问题