Reliably checking if a string is base64 encoded in

2020-03-05 02:27发布

Before I start: Yes, I have checked the other questions and answers on this topic both here and elsewhere.

I have found an example string that the .Net will base64 decode even though it isn't actually base64 encoded. Here is the example:

Rhinocort Aqueous 64mcg/dose Nasal Spray

The .Net method Convert.FromBase64String 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 string as input, it fails with the message invalid input.

Even more interestingly, the source that I thought that belongs to this executable (http://libb64.sourceforge.net/) "decodes" this same string with the same result as I am getting from the .Net Convert.FromBase64String. I will keep looking hoping to find a clue elsewhere but right now I'm stumped.

Any ideas?

标签: c# .net base64
1条回答
forever°为你锁心
2楼-- · 2020-03-05 02:53

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

I recommend you do a check at the beginning. If the input is null or empty then return false.

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

查看更多
登录 后发表回答