I have a key which is Base64 encoded.
While trying to decode I am receiving the following error. The error is thrown by byte[] todecode_byte = Convert.FromBase64String(data);
Error in base64DecodeThe input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.
I am using the below method to decode this:
public string base64Decode(string data)
{
try
{
System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
System.Text.Decoder utf8Decode = encoder.GetDecoder();
byte[] todecode_byte = Convert.FromBase64String(data); // this line throws the exception
int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
char[] decoded_char = new char[charCount];
utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
string result = new String(decoded_char);
return result;
}
catch (Exception e)
{
throw new Exception("Error in base64Decode" + e.Message);
}
}
Encoding.UTF8.GetString(todecode_byte)
after the base64 decoding. But that's aside from the base64 part not working, of course. – Calliope-
isn't a valid character. Where is your string coming from? You might need to change-
characters to either+
or/
. Also it must be a multiple of 4 characters long. Unless I've miscounted, your string has 43 characters. – Heiduc=
characters to make it the right length (i.e. a multiple of 4), but that still leaves the issue with the invalid-
character. You'll need to know the encoding method that they used so you know how to fix it up for .Net. – Heiduc