How to determine if a string is Base64 encoded or not?

  • Thread starter Thread starter Greg
  • Start date Start date
G

Greg

I have been searching around for the best way to do this but haven't found
any solution. I need to check the columns in each row within a DataGridView
to dtermine of it's Base64 encoded or not.

Any ideas?
 
I have been searching around for the best way to do this but haven't found
any solution. I need to check the columns in each row within a
DataGridView
to dtermine of it's Base64 encoded or not.

I would say some trial and error is your best bet. First, try running the
value through the Base64 decode process. If it's not valid Base64, you'll
get an error which you can trap.

In the event you get a "false positive," i.e., a string of characters which
happens to be legal Base64 even though the value wasn't actually Base64
encoded, you can examine the resulting bytes. Since I would guess you're
expecting normal ASCII values, anything less than 32 means it wasn't a
Base64 value. If you don't expect to have accented characters, you can add
"or greater than 127" to that test.

Ultimately, though, something can probably slip by, although I would think
the chances are low.
 
The first thing to check for is the datatype of the column.

It must be one that is capable of holding a string.

The second thing to check for is the length (in charcters) of the value
stored in the column.

It must be an exact multiple of 4 characters. (Remember that it may end in
1, 2 or 3 '=' characters.)

After that, attempt to decode the value.

As Jeff said, you may get false positive so you need a further check to
determine if the result is acceptable. This means that you must have some
knowledge of what the result is likely to contain.
 
Back
Top