error decoding the fields of a search service index that are decoded in base64
Asked Answered
P

1

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

Precision answered 27/3, 2023 at 7:52 Comment(7)
See #50525165 Convert.FromBase64String() expects the proper padding. Add a single '=' character to the end of your input and it works.Samford
Does this answer your question? Convert.FromBase64String() throws "invalid Base-64 string" errorUnloosen
@Unloosen sorry but I keep getting the same error as you can see in the edited postPrecision
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.Unloosen
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.Unloosen
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 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.Unloosen
@Unloosen solved with than last focus thanks you very muchPrecision
P
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);
Precision answered 29/3, 2023 at 7:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.