Mark Tr
Mark Tr

Reputation: 63

Decode base64 string C#

I tried to decode a following base64 string in C#:

PGlmcmFtZSBzcmM9Imh0dHA6Ly9lbWJlZC5yZWR0dWJlLmNvbS8\/aWQ9Mzg1NjAmYmdjb2x
vcj0wMDAwMDAiIGZyYW1lYm9yZGVyPSIwIiB3aWR0aD0iNDM0IiBoZWlnaHQ9IjM0NCIgc2Nyb2xsaW5n
PSJubyIgYWxsb3dmdWxsc2NyZWVuPjwvaWZyYW1lPg==

But i'm getting an 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.

Even if i remove last

=

in the string above but still the same error.

Here is the code i use:

    byte[] decodedBytes = Convert.FromBase64String(embedCode);
    string decodedText = Encoding.UTF8.GetString(decodedBytes);

Why is that?

Thank you.

Upvotes: 1

Views: 14362

Answers (1)

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34152

the correct Base64String is:

PGlmcmFtZSBzcmM9Imh0dHA6Ly9lbWJlZC5yZWR0dWJlLmNvbS8/aWQ9Mzg1NjAmYmdjb2x
vcj0wMDAwMDAiIGZyYW1lYm9yZGVyPSIwIiB3aWR0aD0iNDM0IiBoZWlnaHQ9IjM0NCIgc2Nyb2xsaW5n
PSJubyIgYWxsb3dmdWxsc2NyZWVuPjwvaWZyYW1lPg==

Well this is not a valid Base64String. Base64String can not have \ character. remove this character and it will work

Upvotes: 8

Related Questions