base64 python兼容js_python的base64结果和java不一样

如果你看 一下java8以上的版本中的java.util.Base64.Encoder的源码,你会看到这里涉及两种不同的标准 RFC 2045/ RFC 4648

区别在于最后两个字符的选用、回行符选用、结尾是否配齐等选项。

很多实现不完全是按上面的标准来的,导致结果各异。

private Encoder(boolean isURL, byte[] newline, int linemax, boolean doPadding) {

this.isURL = isURL;

this.newline = newline;

this.linemax = linemax;

this.doPadding = doPadding;

}

/**

* This array is a lookup table that translates 6-bit positive integer

* index values into their "Base64 Alphabet" equivalents as specified

* in "Table 1: The Base64 Alphabet" of RFC 2045 (and RFC 4648).

*/

private static final char[] toBase64 = {

'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',

'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',

'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',

'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',

'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'

};

/**

* It's the lookup table for "URL and Filename safe Base64" as specified

* in Table 2 of the RFC 4648, with the '+' and '/' changed to '-' and

* '_'. This table is used when BASE64_URL is specified.

*/

private static final char[] toBase64URL = {

'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',

'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',

'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',

'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',

'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'

};

private static final int MIMELINEMAX = 76;

private static final byte[] CRLF = new byte[] {'\r', '\n'};

static final Encoder RFC4648 = new Encoder(false, null, -1, true);

static final Encoder RFC4648_URLSAFE = new Encoder(true, null, -1, true);

static final Encoder RFC2045 = new Encoder(false, CRLF, MIMELINEMAX, true);

用是面的类,测试如下

import java.util.Base64;

public class Base64Test {

@Test

public void testBase64() {

String s ="{\"name\": \"Connor\"}";

final byte[] bf = s.getBytes();

System.out.println(s);

System.out.println(Base64.getEncoder().encodeToString(bf));

System.out.println(Base64.getUrlEncoder().encodeToString(bf));

System.out.println(Base64.getMimeEncoder().encodeToString(bf));

}

}

会输出:

{"name": "Connor"}

eyJuYW1lIjogIkNvbm5vciJ9

eyJuYW1lIjogIkNvbm5vciJ9

eyJuYW1lIjogIkNvbm5vciJ9

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值