Reputation: 4789
how to decode a base 64 encoded string encoded using Base64Encode with ATL_BASE64_FLAG_NOPAD flag.
Upvotes: 0
Views: 1893
Reputation: 1502686
Assuming that just means "don't put the =
at the end" you can add the padding directly:
public static string Base64PadEnd(string unpadded)
{
switch(unpadded.Length % 4)
{
case 0: return unpadded;
case 2: return unpadded + "==";
case 3: return unpadded + "=";
default: throw new ArgumentException("Invalid unpadded base64");
}
}
(The way base64 works, you should never end up with an unpadded value with a final block of 1 character.)
After adding the padding, just use Convert.FromBase64String
as usual.
EDIT: As noted in comments, if your base64 string contains whitespace, you should remove that first before adding the padding. A simple text = Regex.Replace(text, @"\s", "")
should do the trick.
Upvotes: 3