Here I am trying to decrpyt a string using private key and getting an error like:
The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters.
The string(i.e string authenticationString) which i am trying to decrpyt is:
Z7W7ja7G+NB3+QsbNnsz7zCkt1xeZ1PQC606wZzQG2McjExT6WjFPDWNpVSqcw1X+K6TERZUK4677m5Z6x9TuxLxyA8h8LmB4dwcJsQZGoVg0mOLsxO6GZmdThLyQOxQgnA7sk4KHLv6DrVswtVzjM/gJouvDKHTC7+NZmjhWwA=
and my code is:
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));
}
and in string au
I am geting a value like:
d��!���u�I|�3��iaȴ{ȱ��q��A��z��ta �i8?�-�[�#�*&��Y^l,�v������ā�\�f�$R�V����&g;�
and am getting this error for a particular username password only. Others are working fine.
Any suggestion?
EDIT:
The error is throwing in this line byte[] decodedStringInBytes = Convert.FromBase64String(encodedValue);
internal virtual string Decode(string encodedValue)
{
byte[] decodedStringInBytes = Convert.FromBase64String(encodedValue);
return Encoding.ASCII.GetString(decodedStringInBytes);
}
EDIT 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");
}
}
Best Answer
The fact that you are getting a value for
au
suggests that it isn't the base-64 decode in the question that is erroring. I'm going to make the assumption that is actually the last line that is failing, and is expecting base-64 - and you are using UTF-8 (backwards, I suspect, which isn't valid). Try: