C#:base64url根据RFC4648 [英] C#: base64url according to RFC4648

查看:819
本文介绍了C#:base64url根据RFC4648的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按在C#中 RFC4648 寻找一个(快)标准实现base64url 。

我发现<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.urltokenen$c$c(v=vs.110).aspx\"相对=nofollow> HttpServerUtility.UrlTokenEn code 但看起来这并不遵循RFC4648(UrlTokenEn code。在增加了许多这表明的 = 被拆除的迹象号结束;见here和<一个href=\"http://stackoverflow.com/questions/7448683/en$c$c-url-from-c-decrypt-url-in-php-extra-characters-being-added-somehow\">here).

例如:

base64编码:


  

Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(AA)); //返回QUE =


base64url编码:


  

HttpServerUtility.UrlTokenEn code(System.Text.Encoding.ASCII.GetBytes(AA));
  //返回QUE1但我希望QUE



解决方案

根据的评论,这听起来像 HttpServerUtility.UrlTokenEn code 做正确的事除了的用于填充的额外的字符。所以,你应该能够做到:

 字符串customBase64 = HttpServerUtility.UrlTokenEn code(数据);
串rfc4648 = customBase64.Substring(0,customBase64.Length - 1);

不过,您应该添加单元测试,以检查它确实使用RFC 4648字母(在相同的方式,RFC 4648)。这有点令人惊讶的是文档是如此稀少:(

I'm looking for a (fast) standard implementation for base64url according to RFC4648 in C#.

I found HttpServerUtility.UrlTokenEncode but it looks like this doesn't follow RFC4648 (UrlTokenEncode adds a number at the end which indicates the number of = signs that were removed; see here and here).

Example:

base64 encoding:

Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes("AA")); //returns "QUE="

base64url encoding:

HttpServerUtility.UrlTokenEncode(System.Text.Encoding.ASCII.GetBytes("AA")); //returns "QUE1" but I would expect "QUE"

解决方案

Based on the comments, it sounds like HttpServerUtility.UrlTokenEncode does the right thing except for the extra character for padding. So you should be able to do:

string customBase64 = HttpServerUtility.UrlTokenEncode(data);
string rfc4648 = customBase64.Substring(0, customBase64.Length - 1);

However, you should add unit tests to check that it really does use the RFC 4648 alphabet (and in the same way as RFC 4648). It's somewhat surprising that the docs are so sparse :(

这篇关于C#:base64url根据RFC4648的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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