关于数组:TextEncodings.Base64Url.Decode与Convert.FromBase64String

TextEncodings.Base64Url.Decode vs Convert.FromBase64String

我正在努力创建一种将生成JWT令牌的方法。该方法的一部分从我的web.config中读取一个值,该值用作"秘密",用于生成用于创建JWT令牌签名的哈希。

1
 

最初,我尝试使用以下命令将"秘密"值转换为字节数组。

1
byte[] key = Convert.FromBase64String(ConfigurationManager.AppSettings["MySecret"]);

但是,到达此行时引发了异常...

该输入不是有效的Base-64字符串,因为它包含非base 64字符,两个以上的填充字符或填充字符中的非法字符。

因此,我研究了OAuth代码,并使用了另一种方法将base64字符串更改为字节数组。

1
byte[] key = TextEncodings.Base64Url.Decode(ConfigurationManager.AppSettings["MySecret"]);

此方法可以正常工作。在我看来,他们在做同样的事情。将Base64文本值更改为字节数组。但是,我一定会丢失一些东西。为什么Convert.FromBase64String失败而TextEncodings.Base64Url.Decode起作用?


将身份验证服务迁移到.NET Core时遇到了同样的事情。我看了我们在先前的实现中使用的库的源代码,实际上区别在于名称本身。

TextEncodings类具有两种类型的文本编码器,Base64TextEncoder和Base64UrlEncoder。后者稍微修改了字符串,以便可以在url中使用base64字符串。

我的理解是,用-和_替换和/很普遍。事实上,我们一直在使用握手令牌进行相同的操作。另外,末尾的填充字符也可以删除。这为我们提供了以下实现(来自源代码):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class Base64UrlTextEncoder : ITextEncoder
{
    public string Encode(byte[] data)
    {
        if (data == null)
        {
            throw new ArgumentNullException("data");
        }

        return Convert.ToBase64String(data).TrimEnd('=').Replace('+', '-').Replace('/', '_');
    }

    public byte[] Decode(string text)
    {
        if (text == null)
        {
            throw new ArgumentNullException("text");
        }

        return Convert.FromBase64String(Pad(text.Replace('-', '+').Replace('_', '/')));
    }

    private static string Pad(string text)
    {
        var padding = 3 - ((text.Length + 3) % 4);
        if (padding == 0)
        {
            return text;
        }
        return text + new string('=', padding);
    }
}