可靠地检查字符串是否是.Net中的base64编码 [英] Reliably checking if a string is base64 encoded in .Net

查看:323
本文介绍了可靠地检查字符串是否是.Net中的base64编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开始之前:是的,我已经在这里和其他地方检查了有关此主题的其他问题和答案。

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

我找到了一个示例字符串,即使.net实际上不是base64编码的,它也可以对base64进行解码。例子如下:

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水溶液64mcg /剂鼻喷剂

The .Net方法Convert.FromBase64String在解码此字符串时不会引发异常,因此我的IsBase64Encoded方法很高兴为此字符串返回true。

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

有趣的是,如果我使用cygwin base64- d命令使用该字符串作为输入,它将失败,并显示消息无效输入

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

更有趣的是,我认为属于此可执行文件的源( http://libb64.sourceforge.net/ )解码相同的字符串,其结果与从.Net Convert.FromBase64String获得的结果相同。我会一直希望在其他地方找到线索,但现在我很困惑。

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.

有什么想法吗?

推荐答案

还有一种更好的解决方案,它也可以检查输入字符串的长度。

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

我建议您在开始时进行检查。如果输入为null或空,则返回 false

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-确定字符串是否是基解码的或

这篇关于可靠地检查字符串是否是.Net中的base64编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆