0

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

enter image description here

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

kintela
  • 1,283
  • 1
  • 14
  • 32
  • See https://stackoverflow.com/questions/50524665/convert-frombase64string-throws-invalid-base-64-string-error Convert.FromBase64String() expects the proper padding. Add a single '=' character to the end of your input and it works. – Austin Mar 27 '23 at 14:28
  • 1
    Does this answer your question? [Convert.FromBase64String() throws "invalid Base-64 string" error](https://stackoverflow.com/questions/50524665/convert-frombase64string-throws-invalid-base-64-string-error) – jps Mar 27 '23 at 15:32
  • @jps sorry but I keep getting the same error as you can see in the edited post – kintela Mar 28 '23 at 07:34
  • Your original example has a length of 263 characters and gets correctly padded with 1 "=" to a length of 264 characters. The problem with the second string is that with a length of 233 characters, it would need 3 padding characters, which is illegal in Base64 encoding. So that string is technically wrong because the last character can't be decoded to anything meaningful. I know that the online decoder can decode it. But if you just remove the last character, you still get an identical result. – jps Mar 28 '23 at 09:16
  • Please check if you really get such a technically wrong string or if there is a mistake and you maybe missed 1 or 2 characters. If the string really comes like this (233 characters) you need to remove the extra characters. You can, if you end up with 3 padding "=", remove the last 4 characters from the string. – jps Mar 28 '23 at 09:19
  • 1
    There are also other approaches, but they all work fine with a string that is correct and just needs padding, but fail in case of wrong/broken input. For example in this [post](https://stackoverflow.com/a/26354677/7329832) Marc calculates the remainder of a division by 4 and only considers the (legal) cases 2 and 3. Your second string has a remainder of 1, which would be `case 1:` in Marcs code. In that case, remove the last character from the string. – jps Mar 28 '23 at 09:26
  • @jps solved with than last focus thanks you very much – kintela Mar 29 '23 at 07:55

1 Answers1

0

The solution is this

private string DecodeBase64(string base64Input)
{
  string base64InputPadded=string.Empty;
  try
  {
    if (string.IsNullOrEmpty(base64Input))
    {
      return "";
    }

    base64InputPadded = base64Input.Replace('_', '/').Replace('-', '+');

    switch (base64InputPadded.Length % 4)
    {
      case 1:
        base64InputPadded = base64Input.Substring(0, base64Input.Length - 1);
        break;
      case 2:
        base64InputPadded += "==";
        break;
      case 3:
        base64InputPadded += "=";
        break;
    }
    byte[] decodedBytes = Convert.FromBase64String(base64InputPadded);

    string decodedString = Encoding.UTF8.GetString(decodedBytes);
kintela
  • 1,283
  • 1
  • 14
  • 32