homepage

This issue tracker has been migrated to GitHub , and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: issue from python in encode base64 with Json Model
Type: behavior Stage:
Components: Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Ramin Farajpour Cami, martin.panter
Priority: normal Keywords:

Created on 2015年09月11日 23:37 by Ramin Farajpour Cami, last changed 2022年04月11日 14:58 by admin. This issue is now closed.

Messages (4)
msg250502 - (view) Author: Ramin Farajpour Cami (Ramin Farajpour Cami) Date: 2015年09月11日 23:37
Hi,
issue from python in encode base64 with Json Model in twice encode with base64 output python different with JAVA and C# , if programmer using rest-service in server side and other programmer using UI(Python Django) in client site , this encode different for encode and decode with match json encode model,
real example :
1- go to https://www.base64encode.org/ 
2- using {"userName":"admin","password":"admin"} (default UTF-8)
3-first encode oupput : "eyJ1c2VyTmFtZSI6ImFkbWluIiwicGFzc3dvcmQiOiJhZG1pbiJ9"
4-again second encode "eyJ1c2VyTmFtZSI6ImFkbWluIiwicGFzc3dvcmQiOiJhZG1pbiJ9" output :
"ZXlKMWMyVnlUbUZ0WlNJNkltRmtiV2x1SWl3aWNHRnpjM2R2Y21RaU9pSmhaRzFwYmlKOQ=="
now second output python json model : 
Microsoft Windows [Version 6.2.9200]
(c) 2012 Microsoft Corporation. All rights reserved.
C:\Users\Matthew F4rr3ll>python
Python 2.7.7 (default, Jun 1 2014, 14:21:57) [MSC v.1500 64 bit (AMD64)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> s1='{"userName":"admin","password":"admin"}'
>>> s2 = s1.encode("base64")
>>> e = s2.encode("base64")
>>> print e
ZXlKMWMyVnlUbUZ0WlNJNkltRmtiV2x1SWl3aWNHRnpjM2R2Y21RaU9pSmhaRzFwYmlKOQo=
>>>
>>>
>>>
now check this output : 
ZXlKMWMyVnlUbUZ0WlNJNkltRmtiV2x1SWl3aWNHRnpjM2R2Y21RaU9pSmhaRzFwYmlKOQo=
ZXlKMWMyVnlUbUZ0WlNJNkltRmtiV2x1SWl3aWNHRnpjM2R2Y21RaU9pSmhaRzFwYmlKOQ==
you see in two encode different in end line in ("o=" != "==")
also i know resolve this but you should fix in end line encode match with other language
Thanks,
Ramin
msg250504 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015年09月12日 00:19
This seems to be as documented. Have a look at the definition of "base64_codec" under <https://docs.python.org/2/library/codecs.html#python-specific-encodings>, which says "the result always includes a trailing '\n' ". In fact, line breaks are also added for longer encodings so that each line is ≤ 76 symbols.
>>> e.decode("base64")
'eyJ1c2VyTmFtZSI6ImFkbWluIiwicGFzc3dvcmQiOiJhZG1pbiJ9\n'
Your alternative encoding does not include that trailing newline. If you don’t want the "MIME Base-64" multiline encoding maybe you should use something like base64.b64encode() directly.
See also Issue 16473 where I proposed a patch to fix the Python 3 documentation that links to the wrong equivalent function.
msg250505 - (view) Author: Ramin Farajpour Cami (Ramin Farajpour Cami) Date: 2015年09月12日 00:26
why any update in python 2.7?
you should using a function this code change :
enc_sec = ZXlKMWMyVnlUbUZ0WlNJNkltRmtiV2x1SWl3aWNHRnpjM2R2Y21RaU9pSmhaRzFwYmlKOQo=
url = re.sub('\o=$','',enc_sec)
result = url.replace('\n','==')
print result
msg250507 - (view) Author: Ramin Farajpour Cami (Ramin Farajpour Cami) Date: 2015年09月12日 00:59
no close plz,
i using python 2.7.3 i get this output without "\n"?
code:
 json_obj='{"userName": "admin", "password": "admin"}'
 enc = json_obj.encode("base64")
 print enc
 enc_sec = re.sub('\n$','',enc)
 print enc_sec
 sec = enc_sec.encode("base64")
 print sec
Output:
{"userName": "admin", "password": "admin"}
eyJ1c2VyTmFtZSI6ICJhZG1pbiIsICJwYXNzd29yZCI6ICJhZG1pbiJ9
ZXlKMWMyVnlUbUZ0WlNJNklDSmhaRzFwYmlJc0lDSndZWE56ZDI5eVpDSTZJQ0poWkcxcGJpSjk=
History
Date User Action Args
2022年04月11日 14:58:20adminsetgithub: 69262
2015年09月12日 00:59:27Ramin Farajpour Camisetmessages: + msg250507
2015年09月12日 00:30:55r.david.murraysetstatus: open -> closed
2015年09月12日 00:26:40Ramin Farajpour Camisetstatus: closed -> open

messages: + msg250505
2015年09月12日 00:20:18martin.pantersetcomponents: - 2to3 (2.x to 3.x conversion tool)
2015年09月12日 00:19:49martin.pantersetstatus: open -> closed

nosy: + martin.panter
messages: + msg250504

resolution: not a bug
2015年09月11日 23:37:45Ramin Farajpour Camicreate

AltStyle によって変換されたページ (->オリジナル) /