Reliable check if base64 string is encoded in .Net

Before you begin: yes, I checked other questions and answers on this topic both here and elsewhere.

I found an example line in which .Net will decode base64, even if it is actually not base64 encoded. Here is an example:

Rhinocort A Water 64mcg / Dose Nasal Spray

The .Net Convert.FromBase64String method does not throw an exception when decoding this string, so my IsBase64Encoded method happily returns true for this string.

Interestingly, if I use the cygwin base64 -d command, using this line as input, it fails with invalid message input.

Even more interesting, the source, which I thought belonged to this executable file ( http://libb64.sourceforge.net/ ), "decodes" the same line with the same as I get from .Net Convert.FromBase64String. I will continue to look for hope to find the key elsewhere, but now I'm at a standstill.

Any ideas?

+4
source share
1 answer

There's a slightly better solution that also checks the length of the input string.

I recommend that you check at the beginning. If the input is empty or empty, return false.

http://www.codeproject.com/Questions/177808/How-to-determine-if-a-string-is-Base-decoded-or

0
source

All Articles