[英]base64 url safe removes =
The following code(using commons codec Base64): 以下代码(使用commons编解码器Base64):
byte[] a = Hex.decodeHex("9349c513ed080dab".toCharArray());
System.out.println(Base64.encodeBase64URLSafeString(a));
System.out.println(Base64.encodeBase64String(a));
gives the following output: 给出以下输出:
k0nFE-0IDas //should be k0nFE-0IDas=
k0nFE+0IDas=
Base64.encodeBase64URLSafeString(a)
returns k0nFE-0IDas
instead of k0nFE-0IDas=
. Base64.encodeBase64URLSafeString(a)
返回k0nFE-0IDas
而不是k0nFE-0IDas=
。 Why is this happening? 为什么会这样?
Why is this happening?
为什么会这样?
Because that's what it's documented to do: 因为这是它记录的内容:
Note: no padding is added.
注意:不添加填充。
The =
characters at the end of a base64 string are called padding. base64字符串末尾的
=
字符称为填充。 They're used to make sure that the final string's length is a multiple of 4 characters - but they're not really required, in terms of information theory, so it's reasonable to remove them so long as you then convert the data back to binary using a method which doesn't expect padding. 它们用于确保最终字符串的长度是4个字符的倍数 - 但就信息理论而言,它们并不是真正需要的,因此只要您将数据转换回二进制文件就可以删除它们。使用不期望填充的方法。 The Apache Codec
Base64
class claims it transparently handles both regular and URL-safe base64, so presumably does handle a lack of padding. Apache Codec
Base64
类声称它透明地处理常规和URL安全的base64,因此可能会处理缺少填充。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.