How to check if a string is Base64 Encoded 
Author Message
 How to check if a string is Base64 Encoded

Hi,

I'm working on some encrypt / decrypt functions, based on the code in the
article at http://www.*-*-*.com/ ;I want to
write a simple function that checks if a given string is Base64 Encoded or
not (so I don't mistakenly encrypt for the second time an already-encrypted
string).

After trawling through the .net documentation I thought I had the answer,
but as you can see (example 3 below), it sometimes reports strings to be
Base64 Encoded when they are NOT.

EXAMPLE 1 - Correctly reports "C1yWOCZ6X9c=" as BEING Base64 Encoded
        Dim binaryData() As Byte
        Try
         binaryData = System.Convert.FromBase64String("C1yWOCZ6X9c=")
         Response.Write("no exception, so string IS Base64")
        Catch Ex as Exception
         Response.Write("exception, so string ISN'T Base64")
        End Try

EXAMPLE 2 - Correctly reports "hallo" as NOT being Base64 Encoded
        Dim binaryData() As Byte
        Try
         binaryData = System.Convert.FromBase64String("hallo")
         Response.Write("no exception, so string IS Base64")
        Catch Ex as Exception
         Response.Write("exception, so string ISN'T Base64")
        End Try

EXAMPLE 3 - Incorrectly reports "good" as BEING Base64 Encoded
        Dim binaryData() As Byte
        Try
         binaryData = System.Convert.FromBase64String("good")
         Response.Write("no exception, so string IS Base64")
        Catch Ex as Exception
         Response.Write("exception, so string ISN'T Base64")
        End Try

Can anyone:

 (a) tell me why the function isn't working,

and (more importantly)

 (b) help me correct the function?

TIA,

JON

PS    If there is some simple trick like "Base 64 Encoded strings always
have an '=' as the last character", I'd be happy to use that to check for
Base64 encoding.



Sun, 28 Aug 2005 03:07:29 GMT  
 How to check if a string is Base64 Encoded
You cannot really tell if the string is base64 encoded or not just by
looking at the string itself. You might conclude that the string can _not_
be base64 encoded because, for example, the length of the string is wrong
(and this is what happens when you pass "hello" to
Convert.FromBase64String()), but that is all.

So the answer is: it is impossible. You have to pass the information about
string encoding along with the string.

Hope it helps
Karol
VB .NET

--
This posting is provided "AS IS" with no warranties, and confers no rights.


Quote:
> Hi,

> I'm working on some encrypt / decrypt functions, based on the code in the
> article at http://www.4guysfromrolla.com/webtech/090501-1.shtml.  I want
to
> write a simple function that checks if a given string is Base64 Encoded or
> not (so I don't mistakenly encrypt for the second time an
already-encrypted
> string).

> After trawling through the .net documentation I thought I had the answer,
> but as you can see (example 3 below), it sometimes reports strings to be
> Base64 Encoded when they are NOT.

> EXAMPLE 1 - Correctly reports "C1yWOCZ6X9c=" as BEING Base64 Encoded
>         Dim binaryData() As Byte
>         Try
>          binaryData = System.Convert.FromBase64String("C1yWOCZ6X9c=")
>          Response.Write("no exception, so string IS Base64")
>         Catch Ex as Exception
>          Response.Write("exception, so string ISN'T Base64")
>         End Try

> EXAMPLE 2 - Correctly reports "hallo" as NOT being Base64 Encoded
>         Dim binaryData() As Byte
>         Try
>          binaryData = System.Convert.FromBase64String("hallo")
>          Response.Write("no exception, so string IS Base64")
>         Catch Ex as Exception
>          Response.Write("exception, so string ISN'T Base64")
>         End Try

> EXAMPLE 3 - Incorrectly reports "good" as BEING Base64 Encoded
>         Dim binaryData() As Byte
>         Try
>          binaryData = System.Convert.FromBase64String("good")
>          Response.Write("no exception, so string IS Base64")
>         Catch Ex as Exception
>          Response.Write("exception, so string ISN'T Base64")
>         End Try

> Can anyone:

>  (a) tell me why the function isn't working,

> and (more importantly)

>  (b) help me correct the function?

> TIA,

> JON

> PS    If there is some simple trick like "Base 64 Encoded strings always
> have an '=' as the last character", I'd be happy to use that to check for
> Base64 encoding.



Sun, 28 Aug 2005 05:19:10 GMT  
 How to check if a string is Base64 Encoded
Hi,

Well, thanks for the info - though it's not the answer I was hoping for!

Just two quick follow-ups:

    i) What lengths of strings cannot be base64, then?
    ii) Based on my limited playing around, it does seem to me that the last
character of a base64 encoded string is invariably an "=" character - is
that always true?

Cheers,

JON



You cannot really tell if the string is base64 encoded or not just by
looking at the string itself. You might conclude that the string can _not_
be base64 encoded because, for example, the length of the string is wrong
(and this is what happens when you pass "hello" to
Convert.FromBase64String()), but that is all.

So the answer is: it is impossible. You have to pass the information about
string encoding along with the string.

Hope it helps
Karol
VB .NET



Sun, 28 Aug 2005 20:21:41 GMT  
 How to check if a string is Base64 Encoded

Quote:

> Well, thanks for the info - though it's not the answer I was hoping for!

> Just two quick follow-ups:

>     i) What lengths of strings cannot be base64, then?

Any length which is a multiple of four.

Quote:
>     ii) Based on my limited playing around, it does seem to me that the last
> character of a base64 encoded string is invariably an "=" character - is
> that always true?

No - only when the input length isn't a multiple of 3. Basically = is a
padder.

Base64 is described in section 6.8 of RFC 2045:
http://www.ietf.org/rfc/rfc2045.txt

--

http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too



Sun, 28 Aug 2005 21:03:11 GMT  
 How to check if a string is Base64 Encoded
Thanks very much to all for their help!

JON



Sun, 28 Aug 2005 21:58:18 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Base64 Encoding

2. multipart/form data to base64 encoding ??

3. Base64 Encode

4. Base64 encoding

5. Base64 encoding

6. Base64 encoding/decoding

7. base64 decoding (encoding probably as well)

8. Base64 Encode for VB?

9. Base64 encoding source needed

10. Base64 Encode function needed

11. base64 encoding

12. base64-encoded content back to file by asp?

 

 
Powered by phpBB® Forum Software