如何检查有效的 Base64 编码字符串 [英] How to check for a valid Base64 encoded string

查看:98
本文介绍了如何检查有效的 Base64 编码字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C# 中是否有其他方法可以查看字符串是否为 Base 64 编码,而不仅仅是尝试对其进行转换并查看是否有错误?我有这样的代码:

Is there a way in C# to see if a string is Base 64 encoded other than just trying to convert it and see if there is an error? I have code code like this:

// Convert base64-encoded hash value into a byte array.
byte[] HashBytes = Convert.FromBase64String(Value);

我想避免在值不是有效的 base-64 字符串时发生的Base-64 字符串中的无效字符"异常.我只想检查并返回 false 而不是处理异常,因为我希望有时这个值不会是 base 64 字符串.在使用 Convert.FromBase64String 函数之前有什么方法可以检查吗?

I want to avoid the "Invalid character in a Base-64 string" exception that happens if the value is not valid base 64 string. I want to just check and return false instead of handling an exception because I expect that sometimes this value is not going to be a base 64 string. Is there some way to check before using the Convert.FromBase64String function?

谢谢!

更新:
感谢您的所有回答.这是到目前为止您都可以使用的扩展方法,它似乎可以确保您的字符串无一例外地通过 Convert.FromBase64String..NET 在转换为 base 64 时似乎忽略所有尾随和结尾空格,因此1234"和1234"都是有效的

Update:
Thanks for all of your answers. Here is an extension method you can all use so far it seems to make sure your string will pass Convert.FromBase64String without an exception. .NET seems to ignore all trailing and ending spaces when converting to base 64 so "1234" is valid and so is " 1234 "

public static bool IsBase64String(this string s)
{
    s = s.Trim();
    return (s.Length % 4 == 0) && Regex.IsMatch(s, @"^[a-zA-Z0-9+/]*={0,3}$", RegexOptions.None);

}

对于那些想知道测试与捕获和异常的性能的人来说,在大多数情况下,对于这个 base 64 的东西,在达到一定长度之前检查比捕获异常要快.长度越小越快

For those wondering about performance of testing vs catching and exception, in most cases for this base 64 thing it is faster to check than to catch the exception until you reach a certain length. The smaller the length faster it is

在我非常不科学的测试中:对于字符长度为 100,000 - 110000 的 10000 次迭代,首先进行测试的速度提高了 2.7 倍.

In my very unscientific testing: For 10000 iterations for character length 100,000 - 110000 it was 2.7 times faster to test first.

对于字符长度为 1 - 16 个字符的 1000 次迭代,总共 16,000 次测试,速度提高了 10.9 倍.

For 1000 iterations for characters length 1 - 16 characters for total of 16,000 tests it was 10.9 times faster.

我确信在某一点上使用基于异常的方法进行测试会变得更好.我只是不知道那是什么时候.

I am sure there is a point where it becomes better to test with the exception based method. I just don't know at what point that is.

推荐答案

更新:对于较新版本的 C#,有更好的替代方案,请参考下面 Tomas 的回答.

Update: For newer versions of C#, there's a much better alternative, please refer to the answer by Tomas below.

识别 Base64 字符串非常容易,因为它只会由字符 'A'..'Z', 'a'..'z', '0'..'9' 组成,'+', '/' 并且通常在末尾填充最多三个 '=',以使长度成为 4 的倍数.但与其进行比较,不如忽略异常(如果发生).

It's pretty easy to recognize a Base64 string, as it will only be composed of characters 'A'..'Z', 'a'..'z', '0'..'9', '+', '/' and it is often padded at the end with up to three '=', to make the length a multiple of 4. But instead of comparing these, you'd be better off ignoring the exception, if it occurs.

这篇关于如何检查有效的 Base64 编码字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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