Reliably checking if a string is base64 encoded in .Net

回眸只為那壹抹淺笑 提交于 2019-12-30 07:11:53

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/26643878/reliably-checking-if-a-string-is-base64-encoded-in-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!