python的base64结果和java不一样

python的base64结果和java不一样

直接上代码
py

data = {

'name' : 'Connor'

}

print(json.dumps(data))

print(b64encode(bytes(json.dumps(data))))

结果

{"name": "Connor"}

eyJuYW1lIjogIkNvbm5vciJ9

java

BASE64Encoder encoder = new BASE64Encoder();

Map<String,String> ret = new HashMap<>();

ret.put("name","Connor");

String s = JSON.toJSONString(ret);

out.println(s);

try {

out.println(encoder.encode(s.getBytes("UTF-8")));

} catch( UnsupportedEncodingException e ) {

e.printStackTrace();

}

结果

{"name":"Connor"}

eyJuYW1lIjoiQ29ubm9yIn0=

求高人指点。。。不胜感激!!!


回答:

{"name":"Connor"}

{"name": "Connor"}

py的多了一个空格


回答:

楼上的好眼力
使用separators来去除空格问题

json.dumps(data, separators=(',', ':'))


回答:

如果你看 一下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


回答:

楼主解决了吗?为何我python输出还是
{"name":"Connor"}
b'eyJuYW1lIjogIkNvbm5vciJ9'
没看到空格不说,多了个b,你怎么没有b啊?python和java的输出还是不一样

以上是 python的base64结果和java不一样 的全部内容, 来源链接: utcz.com/a/159656.html

回到顶部