Issue 25075: issue from python in encode base64 with Json Model (original) (raw)

Issue25075

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:20 admin set github: 69262
2015-09-12 00:59:27 Ramin Farajpour Cami set messages: +
2015-09-12 00:30:55 r.david.murray set status: open -> closed
2015-09-12 00:26:40 Ramin Farajpour Cami set status: closed -> openmessages: +
2015-09-12 00:20:18 martin.panter set components: - 2to3 (2.x to 3.x conversion tool)
2015-09-12 00:19:49 martin.panter set status: open -> closednosy: + martin.pantermessages: + resolution: not a bug
2015-09-11 23:37:45 Ramin Farajpour Cami create