Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue from python in encode base64 with Json Model #69262

Closed
RaminFarajpourCami mannequin opened this issue Sep 11, 2015 · 4 comments
Closed

issue from python in encode base64 with Json Model #69262

RaminFarajpourCami mannequin opened this issue Sep 11, 2015 · 4 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@RaminFarajpourCami
Copy link
Mannequin

RaminFarajpourCami mannequin commented Sep 11, 2015

BPO 25075
Nosy @vadmium

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2015-09-12.00:30:55.177>
created_at = <Date 2015-09-11.23:37:45.993>
labels = ['type-bug', 'invalid']
title = 'issue from python in encode base64 with Json Model'
updated_at = <Date 2015-09-12.00:59:27.416>
user = 'https://bugs.python.org/RaminFarajpourCami'

bugs.python.org fields:

activity = <Date 2015-09-12.00:59:27.416>
actor = 'Ramin Farajpour Cami'
assignee = 'none'
closed = True
closed_date = <Date 2015-09-12.00:30:55.177>
closer = 'r.david.murray'
components = []
creation = <Date 2015-09-11.23:37:45.993>
creator = 'Ramin Farajpour Cami'
dependencies = []
files = []
hgrepos = []
issue_num = 25075
keywords = []
message_count = 4.0
messages = ['250502', '250504', '250505', '250507']
nosy_count = 2.0
nosy_names = ['martin.panter', 'Ramin Farajpour Cami']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue25075'
versions = ['Python 2.7']

@RaminFarajpourCami
Copy link
Mannequin Author

RaminFarajpourCami mannequin commented Sep 11, 2015

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

@RaminFarajpourCami RaminFarajpourCami mannequin added topic-2to3 type-bug An unexpected behavior, bug, or error labels Sep 11, 2015
@vadmium
Copy link
Member

vadmium commented Sep 12, 2015

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 bpo-16473 where I proposed a patch to fix the Python 3 documentation that links to the wrong equivalent function.

@RaminFarajpourCami
Copy link
Mannequin Author

RaminFarajpourCami mannequin commented Sep 12, 2015

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

@RaminFarajpourCami RaminFarajpourCami mannequin reopened this Sep 12, 2015
@RaminFarajpourCami
Copy link
Mannequin Author

RaminFarajpourCami mannequin commented Sep 12, 2015

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=

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants