gpt4 book ai didi

base64 - Base64 编码时删除尾随 "="

转载 作者:行者123 更新时间:2023-12-02 11:17:32 26 4
gpt4 key购买 nike

我注意到每当我对字符串进行 Base64 编码时,都会在末尾附加一个“=”。我可以删除这个字符,然后通过将其添加回去来可靠地解码它吗?或者这很危险吗?换句话说,“=”总是附加,还是仅在某些情况下附加?

我希望编码的字符串尽可能短,这就是为什么我想知道是否可以删除“=”字符并在解码之前将其添加回来。

最佳答案

= 是填充。 <!------------>

Wikipedia

An additional pad character isallocated which may be used to forcethe encoded output into an integermultiple of 4 characters (orequivalently when the unencoded binarytext is not a multiple of 3 bytes) ;these padding characters must then bediscarded when decoding but stillallow the calculation of the effectivelength of the unencoded text, when itsinput binary length would not be amultiple of 3 bytes (the last non-padcharacter is normally encoded so thatthe last 6-bit block it representswill be zero-padded on its leastsignificant bits, at most two padcharacters may occur at the end of theencoded stream).

如果您控制另一端,则可以在传输时将其移除,然后在解码之前重新插入(通过检查字符串长度)。
请注意,数据在传输中不会是有效的 Base64。

另外,另一位用户指出(与 PHP 用户相关):

Note that in PHP base64_decode will accept strings without padding, hence if you remove it to process it later in PHP it's not necessary to add it back. – Mahn Oct 16 '14 at 16:33

因此,如果您的目标是 PHP,您可以安全地去除填充并进行解码,而无需进行复杂的计算。

关于base64 - Base64 编码时删除尾随 "=",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4492426/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com