简体   繁体   English

使用+编码引导时,Base64解码中断

[英]Base64 Decoding breaks when encoding leads with +

Everytime i encode a string using Base64 and a + is added, the decoding will fail regarding the length of the string is invalid. 每次使用Base64对字符串进行编码并添加+ ,解码将失败,因为字符串的长度无效。 If the encoding does not have the leading + it'll decode just fine. 如果编码没有前导+它将解码就好了。 Can anyone please explain why this happens? 任何人都可以解释为什么会这样吗? What would cause the + sign to be generated on some cases? 什么会导致在某些情况下生成+号? Example below, this string was encoded but can't be decoded. 以下示例中,此字符串已编码但无法解码。

+ueJ0q91t5XOnFYP8Xac3A== 

An example of a parameter i am passing would be in the following format prior to encoding, 123_true or 123_false. 我传递的参数示例在编码之前采用以下格式,123_true或123_false。 Would the "_" be causing the random issue with the "+" showing up? “_”是否会导致出现“+”的随机问题?

+ is one of the regular base64 characters , used when the 6 bits being encoded have a value of 62. +是常规base64字符之一 ,当编码的6位值为62时使用。

My guess is that you're putting this in the query parameter of a URL, where + is the escaped value of space. 我的猜测是你把它放在一个URL的查询参数中,其中+是空间的转义值。 For that use case, you should use a URL-safe base64 encoding instead: 对于该用例,您应该使用URL安全的base64编码

Using standard Base64 in URL requires encoding of '+', '/' and '=' characters into special percent-encoded hexadecimal sequences ('+' becomes '%2B', '/' becomes '%2F' and '=' becomes '%3D'), which makes the string unnecessarily longer. 在URL中使用标准Base64需要将'+','/'和'='字符编码为特殊的百分比编码的十六进制序列('+'变为'%2B','/'变为'%2F'并且'='变为'%3D'),这使得字符串不必要地更长。

For this reason, modified Base64 for URL variants exist, where the '+' and '/' characters of standard Base64 are respectively replaced by '-' and '_', so that using URL encoders/decoders is no longer necessary and have no impact on the length of the encoded value, leaving the same encoded form intact for use in relational databases, web forms, and object identifiers in general. 出于这个原因,存在针对URL变体的修改后的Base64,其中标准Base64的'+'和'/'字符分别被' - '和'_'替换,因此不再需要使用URL编码器/解码器而且没有对编码值的长度的影响,保留相同的编码形式,以便在关系数据库,Web表单和对象标识符中使用。 Some variants allow or require omitting the padding '=' signs to avoid them being confused with field separators, or require that any such padding be percent-encoded. 某些变体允许或要求省略填充'='符号以避免它们与字段分隔符混淆,或要求任何此类填充都是百分比编码。 Some libraries (like org.bouncycastle.util.encoders.UrlBase64Encoder) will encode '=' to '.'. 一些库(如org.bouncycastle.util.encoders.UrlBase64Encoder)将'='编码为'。'。

Exactly which path you choose here will depend on whether or not you control both sides - if you do, using the modified decodabet is probably the best plan. 你在这里选择的路径究竟取决于你是否控制双方 - 如果你这样做,使用修改后的decodabet可能是最好的计划。 Otherwise, you need to just escape the query parameter. 否则,您只需要转义查询参数。

Example below, this string was encoded but can't be decoded. 以下示例中,此字符串已编码但无法解码。

+ueJ0q91t5XOnFYP8Xac3A== + ueJ0q91t5XOnFYP8Xac3A ==

That's not true, in itself: 事实并非如此:

byte[] bytes = Convert.FromBase64String("+ueJ0q91t5XOnFYP8Xac3A==");

works fine... suggesting that it's the propagation of the string that's broken, which is in line with what I've said above. 工作得很好...这表明它是被破坏的弦的传播,这与我上面所说的一致。

Similar to the PHP solution for this problem, you can replace + , / and = with the safe characters - , _ , and , 类似于针对此问题的PHP 解决方案 ,您可以将+/=替换为安全字符-_,

string safeBase64= base64.Replace('+', '-').Replace('/', '_').Replace('=', ',')

Just before decoding you can replace back the original characters: 在解码之前,您可以替换原始字符:

string base64 = safeBase64.Replace('-','+').Replace('_','/').Replace(',','=')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM