Home All Groups Group Topic Archive Search About

How to figure out if data is base64encoded.

Author
27 Jun 2006 11:25 PM
Jeremy Chapman
Is there some way to programatically tell if a string is base64 encoded?

Author
28 Jun 2006 1:52 AM
Carl Daniel [VC++ MVP]
Jeremy Chapman wrote:
> Is there some way to programatically tell if a string is base64
> encoded?

In general, no.

However, if a string contains only the base64 characrter set [a-zA-Z0-9+/],
ends in 0, 1 or 2 equals signs ('=') and is a multiple of 4 characters in
total length, then it is a valid base64 encoding of something.  Of course,
that could occur completely by coincidence - the string "c3U=" is a valid
Base64 string, but it's not the encoding of anything particularly useful.

-cd
Author
28 Jun 2006 2:34 AM
William Stacey [MVP]
I think the easiest and fastest way is to just try and decode it and catch
falure.  If it fails, it is not base64 or is invalid base64 for some reason.
Then you can tell in 1 pass.  If you try to walk it first just see if it is
valid, you end up with 2 passes (1 for the test and 1 for the actual
decode) - so IMO, it is not worth it anyway.

--
William Stacey [MVP]

Show quote
"Jeremy Chapman" <please@Idontlikespam> wrote in message
news:u8Ba7DkmGHA.3980@TK2MSFTNGP02.phx.gbl...
| Is there some way to programatically tell if a string is base64 encoded?
|
|

AddThis Social Bookmark Button