V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
awanganddong
V2EX  ›  程序员

base64 编码有什么不同吗

  •  
  •   awanganddong · 2021-06-03 23:03:14 +08:00 · 3057 次点击
    这是一个创建于 1028 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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


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

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

    然后他那边想一步步对结果,但是 php 转换 2 进制比较麻烦
    就直接用 go 和他对结果了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3407 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 11:15 · PVG 19:15 · LAX 04:15 · JFK 07:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.