Base32解码
我有一个 base32 字符串,需要将其转换为字节数组。 我在 .NET 框架中找不到转换方法。 我可以找到适用于 Base64 的方法,但找不到适用于 Base32 的方法。
Convert.FromBase64String
– 对于 base32 来说这样的东西将是完美的。
框架中有这样的方法还是我必须自己推出?
I have a base32 string which I need to convert to a byte array. And I'm having trouble finding a conversion method in the .NET framework. I can find methods for base64 but not for base32.
Convert.FromBase64String
– something like this for base32 would be perfect.
Is there such a method in the framework or do I have to roll my own?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我需要一个 Base32 编码器/解码器,所以今天下午我花了几个小时把它放在一起。 我相信它符合此处列出的标准: https://www.rfc-editor .org/rfc/rfc4648#section-6。
I had a need for a base32 encoder/decoder, so I spent a couple hours this afternoon throwing this together. I believe it conforms to the standards listed here: https://www.rfc-editor.org/rfc/rfc4648#section-6.
检查 .NET 的
FromBase32String
实现,发现 此处。编辑:上面的链接已失效; 您可以在 archive.org
实际代码如下:
Check this
FromBase32String
implementation for .NET found here.Edit: The above link was dead; you can find an archived copy at archive.org
The actual code read:
这是一个非常古老的问题,但我偶然发现它想要 OTP 代币同样的东西。 事实证明,NuGet 上的 OTP.NET 包中内置了 Base 32 功能:
反之亦然:
This is a really old question, but I happened to stumble on it wanting the same thing for OTP tokens. It turns out that there is base 32 functionality built into the OTP.NET package on NuGet:
The reverse is also possible:
这是我的编码和解码函数。 我觉得它们比其他建议更短、更简洁。 因此,如果您需要一个小的,请尝试这些。
编辑:根据@Patrick的修复更新
Here are my functions for encoding and decoding. I feel that they are much shorter and concise than the other suggestions. So if you need a small one, try these.
EDIT: updated according to @Patrick's fix
我编写了一些基于灵活标准的各种 Base32 和 Base64 编码/解码方法的实现。 值得注意的是:base64url(根据 rfc4648)及其等效的 base32。
默认情况下,Base32Url 类仅使用字符 A 到 Z 和 2 到 7 进行编码。不使用连字符、下划线、加号、斜杠或等号,使其在几乎所有情况下都可用作 URL 令牌。 Base32Url 还支持自定义字母、区分大小写/不敏感、填充/无填充等。
这是在代码项目上发布的。
I've written some flexible standards based implementations of various Base32 and Base64 encoding/decoding methods. Notably: base64url (per rfc4648) and its base32 equivalent.
By default the Base32Url class encodes with only the characters A to Z and 2 to 7. No hyphens, underscores, pluses, slashes or equals are used, making it usable as a URL token in almost all circumstances. Base32Url also supports custom alphabets, case sensitivity/insensitivity, padding/no-padding etc.
This is posted up on code project.
这是我快速写下的解决方案。 它仅适用于 8 Base32 字符的倍数的字符串。 不过确实有效。
Here's my quickly jotted solution. It only works on strings that are a multiple of 8 base32 characters. Does work, though.
我为 VB.NET 提出了自己的通用 Base32 编码器/解码器实现。 我已经通过独立网站验证了结果,因此它看起来确实相当准确。
欢迎任何有关如何改进代码的评论。
下面是将 123456789 转换为 Base32 的示例
I have come up with my own generic Base32 encoder/decoder implementation for VB.NET. I have verified the results through independent web sites so it does appear to be quite accurate.
Any comments on how improvements could be made to the code will be welcomed.
Here is an example of converting 123456789 to Base32