While Convert.FromBase64String(base64).Length approach works, if you just want to find the length of the decoded Base64 bytes you could easily calculate it with out actually needing to decode your input and therefore save memory.
In general 4 Base64 characters are converted to 3 bytes. If the amount of bytes is not divisible by 3 '=' characters are used as padding to get to a full 4 character block. Also whitespace is ignored, during decoding.
int DecodedLength(string base64)
{
var chars = 0;
var padding = 0;
for (int i = 0; i < base64.Length; i++)
{
var ch = base64[i];
if (!char.IsWhiteSpace(ch)) { chars++; }
if (ch == '=') { padding++; }
}
return chars / 4 * 3 - padding;
}
Or if you want a LINQ oneliner, which is however slightly less performant as it's iterating twice over the input and relying on enumerator calls:
int DecodedLength(string base64)
{ return (base64.Count(ch => !char.IsWhiteSpace(ch)) / 4 * 3 - base64.Count(ch => ch == '=')); }
Console.WriteLine($"{Convert.FromBase64String("VGVzdGluZw==").Length}");is a simple solution.