Convert.ToBase64String(byte [])和HttpServerUtility.UrlTokenEncode(byte [])之间有什么区别? [英] What is the difference between Convert.ToBase64String(byte[]) and HttpServerUtility.UrlTokenEncode(byte[])?

查看:87
本文介绍了Convert.ToBase64String(byte [])和HttpServerUtility.UrlTokenEncode(byte [])之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Web API项目中删除对 System.Web.dll 的依赖,但是偶然发现了对

I'm trying to remove a dependence on System.Web.dll from a Web API project, but have stumbled on a call to HttpServerUtility.UrlTokenEncode(byte[] input) (and its corresponding decode method) that I don't know what to replace with to ensure backwards compatibility. The documentation says that this method

使用基数64位将字节数组编码为其等效的字符串表示形式,可用于在URL上进行传输.

Encodes a byte array into its equivalent string representation using base 64 digits, which is usable for transmission on the URL.

我尝试用 Convert替换.ToBase64String(byte []输入) (及其相应的解码方法),在文档中有非常相似的描述:

I tried substituting with Convert.ToBase64String(byte[] input) (and its corresponding decode method), which is very similarly described in the docs:

将8位无符号整数数组转换为其等效的字符串表示形式,并以64位为底的数字进行编码.

Converts an array of 8-bit unsigned integers to its equivalent string representation that is encoded with base-64 digits.

但是,它们似乎并不完全相同;当使用 Convert.FromBase64String(string input)解码使用 HttpServerUtility 编码的字符串时,出现异常,指出

However, they don't seem to be entirely equivalent; when using Convert.FromBase64String(string input) to decode a string encoded with HttpServerUtility, I get an exception stating

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

The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.

这两个转换实用程序有什么区别?消除对 System.Web.HttpServerUtility 的依赖的正确方法是什么?

What is the difference between these two conversion utilities? What's the correct way to remove this dependence on System.Web.HttpServerUtility?

一些用户建议这是此版本的副本,但我不同意.这个问题是关于以一般 的url安全方式对字符串进行base-64编码,但是我需要重现 HttpServerUtility exact行为>,但不依赖于 System.Web .

Some users have suggested that this is a duplicate of this one, but I disagree. That question is about base-64-encoding a string in a url-safe manner in general, but I need to reproduce the exact behavior of HttpServerUtility but without a dependency on System.Web.

推荐答案

我将DGibbs用作他们的单词,并已使用 来源.事实证明,在 HttpServerUtility 方法中发生了以下情况:

I took DGibbs on their word and Used the Source. It turns out the following happens in the HttpServerUtility methods:

  1. 使用 System.Convert 将输入转换为Base64.

  1. Use System.Convert to convert the input to Base64.

-替换 + ,并用 _ 替换/.例如: Foo + bar/=== 变为 Foo-bar _ === .

Replace + by - and / by _. Example: Foo+bar/=== becomes Foo-bar_===.

在字符串末尾替换任意数量的 = ,并用整数表示它们的数量.例如: Foo-bar _ === 变为 Foo-bar_3 .

Replace any number of = at the end of the string, with an integer denoting how many they were. Example: Foo-bar_=== becomes Foo-bar_3.

从Base64解码

  1. 用相同数量的 = 符号替换字符串末尾的数字.示例: Foo-bar_3 变为 Foo-bar _ === .

  1. Replace the digit at the end of the string by the same number of = signs. Example: Foo-bar_3 becomes Foo-bar_===.

+ 替换-,并用/替换 _ .例如: Foo-bar _ === 变为 Foo + bar/=== .

Replace - by + and _ by /. Example: Foo-bar_=== becomes Foo+bar/===.

使用 System.Convert 解码来自Base64的预处理输入.

Use System.Convert to decode the preprocessed input from Base64.

这篇关于Convert.ToBase64String(byte [])和HttpServerUtility.UrlTokenEncode(byte [])之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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