Error converting base64 to String

1

I have a base64 string but the C # compiler says it is not valid, I get the following error:

  

The entry is not a valid Base 64 string because it contains a non-base 64 character, more than two fill-in characters, or an illegal character between the fill-in characters.

My code is as follows:

String s_data = "RmluYWwtUmVjaXBpZW50OiByZmM4MjI7IGd1YXJhbmFicmFzaWxAZ3VhcmFuYXN0b2NrLmNvbQ0KQWN0aW9uOiBmYWlsZWQNClN0YXR1czogNS4wLjANCkRpYWdub3N0aWMtQ29kZTogc210cDsgVGhlIGVtYWlsIGFjY291bnQgdGhhdCB5b3UgdHJpZWQgdG8gcmVhY2ggZG9lcyBub3QgZXhpc3QuIFBsZWFzZSB0cnkgZG91YmxlLWNoZWNraW5nIHRoZSByZWNpcGllbnQncyBlbWFpbCBhZGRyZXNzIGZvciB0eXBvcyBvciB1bm5lY2Vzc2FyeSBzcGFjZXMuIExlYXJuIG1vcmUgYXQgaHR0cHM6Ly9zdXBwb3J0Lmdvb2dsZS5jb20vbWFpbC8_cD1Ob1N1Y2hVc2VyIG40c29yMzk2MDY5M3dtaC4zNCAtIGdzbXRwDQpMYXN0LUF0dGVtcHQtRGF0ZTogV2VkLCAwNyBNYXIgMjAxOCAwOTo1MToxNiAtMDgwMCAoUFNUKQ0K";
byte[] data = Convert.FromBase64String(s_data);
string decodedString = Encoding.UTF8.GetString(data);

I've tested the string of% with% on that site and it converts normally. I have already converted other data to strings but this is giving problem.

    
asked by anonymous 07.03.2018 / 22:41

1 answer

1

It happens because in the conversion to Base64 sometimes characters change from + and / to - and _

link

Following example working link

String s_data = "RmluYWwtUmVjaXBpZW50OiByZmM4MjI7IGd1YXJhbmFicmFzaWxAZ3VhcmFuYXN0b2NrLmNvbQ0KQWN0aW9uOiBmYWlsZWQNClN0YXR1czogNS4wLjANCkRpYWdub3N0aWMtQ29kZTogc210cDsgVGhlIGVtYWlsIGFjY291bnQgdGhhdCB5b3UgdHJpZWQgdG8gcmVhY2ggZG9lcyBub3QgZXhpc3QuIFBsZWFzZSB0cnkgZG91YmxlLWNoZWNraW5nIHRoZSByZWNpcGllbnQncyBlbWFpbCBhZGRyZXNzIGZvciB0eXBvcyBvciB1bm5lY2Vzc2FyeSBzcGFjZXMuIExlYXJuIG1vcmUgYXQgaHR0cHM6Ly9zdXBwb3J0Lmdvb2dsZS5jb20vbWFpbC8_cD1Ob1N1Y2hVc2VyIG40c29yMzk2MDY5M3dtaC4zNCAtIGdzbXRwDQpMYXN0LUF0dGVtcHQtRGF0ZTogV2VkLCAwNyBNYXIgMjAxOCAwOTo1MToxNiAtMDgwMCAoUFNUKQ0K";
string converted = s_data.Replace('-', '+');
converted = converted.Replace('_', '/');
byte[] data = Convert.FromBase64String(converted);
string decodedString = Encoding.UTF8.GetString(data);
    
07.03.2018 / 23:56