I have this
private string DecodeBase64(string base64Input)
{
try
{
if (string.IsNullOrEmpty(base64Input))
{
return "";
}
byte[] decodedBytes = Convert.FromBase64String(base64Input);
string decodedString = Encoding.UTF8.GetString(decodedBytes);
// Paso 2: Decodificar la URL
string originalString = WebUtility.UrlDecode(decodedString);
return originalString;
}
catch (Exception ex)
{
return "";
}
}
To decode the data that comes to me from a search in an Azure search service index and it works for many of the entries received but in some cases it gives me an error and for example when this entry arrives
aHR0cHM6Ly9zYW9mZXJ0YXNoaXN0b3JpY29pbmRleC5ibG9iLmNvcmUud2luZG93cy5uZXQvb2ZlcnRhcy8wMDUtMjNfSU5URVJCSUFLX0RPJTIwUEMlMjBhZGVjdWFjaW9uJTIwdHVuZWwlMjBBdmFuemFkYS9Eb2NfVGVjbmljYS9BbmVqb3MvQW5lam8lMjAyX0N1cnJpY3VsYSUyMHZpdGFydW0vVEVLSUEvQ1ZfR09SS0FfJTIwQUxWRUFSLnBkZg2
It gives me an incorrect entry error,
The 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.
However if I code it using this website, everything goes well
It seems that the string to be decoded has to have a length that is a multiple of 4.
Convert.FromBase64String() throws "invalid Base-64 string" error
I have tried this
string base64InputPadded=string.Empty;
base64InputPadded = base64Input.Replace('-', '+').Replace('_', '/').PadRight(4 * ((base64Input.Length + 3) / 4), '=');
byte[] decodedBytes = convert.FromBase64String(base64InputPadded);
string decodedString = Encoding.UTF8.GetString(decodedBytes);
but when I get this string for example "aHR0cHM6Ly9zYW9mZXJ0YXNoaXN0b3JpY29pbmRleC5ibG9iLmNvcmUud2luZG93cy5uZXQvb2ZlcnRhcy8xMTQtMjJfTUlUTUFfQWN0dWFsaXouJTIwUFR5QyUyMExvcyUyMFJhYmFub3MtRnVlbnNhdWNvJTIwKFNvcmlhKS9Eb2NfQWRtaW5pc3RyYXRpdmEvQW5leG8lMjBJJTIwRFJDJTIwREVVQy5kb2N40" whose length is 233 characters transforms it to this other one of 236 characters "aHR0cHM6Ly9zYW9mZXJ0YXNoaXN0b3JpY29pbmRleC5ibG9iLmNvcmUud2luZG93cy5uZXQvb2ZlcnRhcy8xMTQtMjJfTUlUTUFfQWN0dWFsaXouJTIwUFR5QyUyMExvcyUyMFJhYmFub3MtRnVlbnNhdWNvJTIwKFNvcmlhKS9Eb2NfQWRtaW5pc3RyYXRpdmEvQW5leG8lMjBJJTIwRFJDJTIwREVVQy5kb2N40==="
but i still get the same error
Any idea, please?
Thanks
case 1:
in Marcs code. In that case, remove the last character from the string. – Unloosen