base64 编码有什么不同吗

2021-06-03 23:03:14 +08:00
 awanganddong

1.通过线上 base64 编码和 PHP 本地 base64_decode 编码得到的结果是不一样的 2.编码得到的都是二进制文件 3.线上的结果和 ios 是一致的,想问下这是为什么呢。php 是 MIME base64 编码的。

3082 次点击
所在节点    程序员
16 条回复
ysc3839
2021-06-03 23:06:47 +08:00
发代码看看吧,感觉是字符串编码问题。
hanxiV2EX
2021-06-03 23:08:24 +08:00
utf8 和 gbk 的问题吧
h82258652
2021-06-03 23:26:52 +08:00
是不是换行了,我记得有些 base64 库是会添加换行的
zjsxwc
2021-06-04 08:26:36 +08:00
base64_decode 不是解码吗?
难道不是应该 base64_encode 来编码吗?
limuyan44
2021-06-04 08:46:56 +08:00
打印出来不就知道了,有些库字符会自己转移
bruce0
2021-06-04 09:04:44 +08:00
是不是 base64 个 url_safe base64 的区别 我之前遇到过这个问题,被坑了一次
bruce0
2021-06-04 09:05:04 +08:00
@bruce0 是不是 base64 和 url_safe base64 的区别 我之前遇到过这个问题,被坑了一次
liuidetmks
2021-06-04 10:18:04 +08:00
一般是换行的区别
AoEiuV020
2021-06-04 10:49:40 +08:00
不贴代码没法解决,随便踩个坑都可能出现问题,写个 demo 验证下然后贴出来看看,
ayanmw
2021-06-04 12:09:28 +08:00
base64 有 Std 和 URL 两种 ,主要是 对 +/ 或者 -_ 多出的两个符号处理不一致, 还有 结尾的 也不一样,所以还有 padding 和 nopadding (结尾的=号),如果是 URL 还要考虑 URL_ENCODE
具体看:
RFC 4648. section 3.2.


PS : 我是从 golang 的 base64 了解到这些的.
no1xsyzy
2021-06-04 12:28:51 +08:00
一种是编码字符集的问题,注意一点:base64 是对一堆字节进行编码,通常连长度都相当不一致
一种是字母表不一致的问题,通常仅限于 +/ vs -_ ,硬要说 padding vs no padding 也可以理解为 padding 字符串是 '=' 还是 ''
bertonzh
2021-06-04 14:03:48 +08:00
可以搞一个几个字节的二进制文件,把文件字节和 base64 结果贴出来看看。
base64 算法很简单,完全可以人肉分析。
elfsundae
2021-06-06 18:09:34 +08:00
1.通过线上 base64 编码和 PHP 本地 base64_decode 编码得到的结果是不一样的
—-
php 的编码函数是 base64_encode 不是 base64_decode
elfsundae
2021-06-06 18:12:00 +08:00
另外,需要 urlsafe base64 编解码的话,我有个轮子 :) https://github.com/ElfSundae/urlsafe-base64
LLaMA2
2021-06-07 11:28:47 +08:00
请参考 Java 中关于 Base64 的说明,各语言的各种实现有区别。

This class consists exclusively of static methods for obtaining encoders and decoders for the Base64 encoding scheme. The implementation of this class supports the following types of Base64 as specified in RFC 4648 and RFC 2045 .
Basic
Uses "The Base64 Alphabet" as specified in Table 1 of RFC 4648 and RFC 2045 for encoding and decoding operation. The encoder does not add any line feed (line separator) character. The decoder rejects data that contains characters outside the base64 alphabet.
URL and Filename safe
Uses the "URL and Filename safe Base64 Alphabet" as specified in Table 2 of RFC 4648 for encoding and decoding. The encoder does not add any line feed (line separator) character. The decoder rejects data that contains characters outside the base64 alphabet.
MIME
Uses "The Base64 Alphabet" as specified in Table 1 of RFC 2045 for encoding and decoding operation. The encoded output must be represented in lines of no more than 76 characters each and uses a carriage return '\r' followed immediately by a linefeed '\n' as the line separator. No line separator is added to the end of the encoded output. All line separators or other characters not found in the base64 alphabet table are ignored in decoding operation.
Unless otherwise noted, passing a null argument to a method of this class will cause a NullPointerException to be thrown.
Since:
1.8
Author:
Xueming Shen
awanganddong
2021-06-08 22:18:14 +08:00
问题解决了

我和 ios 做接口加密
异或运算之后对结果 base64_safe 操作

然后他那边想一步步对结果,但是 php 转换 2 进制比较麻烦
就直接用 go 和他对结果了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/781232

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX