簡體   English   中英

為什么我對相同文本的 python 和 java base64 編碼有不同的結果?

[英]Why I have different results for python and java base64 encode for the same text?

我在 python 和 java 中有兩個代碼如下,但是運行它們得到不同的結果,發生了什么?

python2.7代碼:

#encoding:utf-8
import json
import base64

st_test = {"test":"測試內容"}
body = json.dumps(st_test,ensure_ascii=False)
res = base64.b64encode(body)
prin res 
#eyJ0ZXN0IjogIua1i+ivleWGheWuuSJ9

爪哇代碼:

import java.util.Base64;

body = "{\"test\":\"測試內容\"}";
String body64 = Base64.getEncoder().encodeToString(body.getBytes("UTF-8")) ;
System.out.println(body64);
//eyJ0ZXN0Ijoi5rWL6K+V5YaF5a65In0=

您有兩個不同的字符串 - Java在以下之后沒有空格:

如果我刪除空間

body = body.replace(' ', '')

然后我得到相同的代碼


import json
import base64

st_test = {"test": "測試內容"}
body = json.dumps(st_test, ensure_ascii=False)
print body

body = body.replace(' ', '')
print body

res = base64.b64encode(body)
print res
print (res == 'eyJ0ZXN0Ijoi5rWL6K+V5YaF5a65In0=')

結果

{"test": "測試內容"}
{"test":"測試內容"}
eyJ0ZXN0Ijoi5rWL6K+V5YaF5a65In0=
True

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM