当编码以 + 开头时,Base64 解码中断
Base64 Decoding breaks when encoding leads with +
每次我使用Base64编码一个字符串并添加一个+
时,解码将失败,因为字符串的长度是无效的。如果编码没有前导 +
它会解码得很好。谁能解释为什么会这样?什么会导致在某些情况下生成 +
符号?下面的示例,此字符串已编码但无法解码。
+ueJ0q91t5XOnFYP8Xac3A==
我传递的参数示例在编码之前采用以下格式,123_true 或 123_false。 “_”是否会导致出现“+”的随机问题?
+
是常规 base64 characters 之一,当被编码的 6 位的值为 62 时使用。
我的猜测是您将它放在 URL 的查询参数中,其中 +
是 space 的转义值。对于该用例,您应该改用 URL-safe base64 encoding:
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.
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. 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 '.'.
你在这里选择哪条路径将取决于你是否控制双方——如果你控制了,使用修改后的 decodabet 可能是最好的计划。否则,您只需转义查询参数即可。
Example below, this string was encoded but can't be decoded.
+ueJ0q91t5XOnFYP8Xac3A==
事实并非如此:
byte[] bytes = Convert.FromBase64String("+ueJ0q91t5XOnFYP8Xac3A==");
工作正常...提示是字符串的传播坏了,符合我上面说的。
类似PHP solution这道题,可以把+
,/
,=
换成安全字符-
、_
和,
string safeBase64= base64.Replace('+', '-').Replace('/', '_').Replace('=', ',')
在解码之前你可以替换回原来的字符:
string base64 = safeBase64.Replace('-','+').Replace('_','/').Replace(',','=')
每次我使用Base64编码一个字符串并添加一个+
时,解码将失败,因为字符串的长度是无效的。如果编码没有前导 +
它会解码得很好。谁能解释为什么会这样?什么会导致在某些情况下生成 +
符号?下面的示例,此字符串已编码但无法解码。
+ueJ0q91t5XOnFYP8Xac3A==
我传递的参数示例在编码之前采用以下格式,123_true 或 123_false。 “_”是否会导致出现“+”的随机问题?
+
是常规 base64 characters 之一,当被编码的 6 位的值为 62 时使用。
我的猜测是您将它放在 URL 的查询参数中,其中 +
是 space 的转义值。对于该用例,您应该改用 URL-safe base64 encoding:
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.
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. 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 '.'.
你在这里选择哪条路径将取决于你是否控制双方——如果你控制了,使用修改后的 decodabet 可能是最好的计划。否则,您只需转义查询参数即可。
Example below, this string was encoded but can't be decoded.
+ueJ0q91t5XOnFYP8Xac3A==
事实并非如此:
byte[] bytes = Convert.FromBase64String("+ueJ0q91t5XOnFYP8Xac3A==");
工作正常...提示是字符串的传播坏了,符合我上面说的。
类似PHP solution这道题,可以把+
,/
,=
换成安全字符-
、_
和,
string safeBase64= base64.Replace('+', '-').Replace('/', '_').Replace('=', ',')
在解码之前你可以替换回原来的字符:
string base64 = safeBase64.Replace('-','+').Replace('_','/').Replace(',','=')