Python Forum
how to encode and decode same value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to encode and decode same value
#1
Hi.
I obtain these data:
ADhlamtsOXdreXE2eAA0VFpWTGJBWDdSZW83d3AwYnJnNlVnSEpRcTlvR0wwMTlldUhSelJ6bXRtTTRnbmUxU
I'm tring to decode and then encode to check that I have the same result but the second python program not gives the result of the first. I'll explain better with examples:


 $ more decode.py
import base64
code = "ADhlamtsOXdreXE2eAA0VFpWTGJBWDdSZW83d3AwYnJnNlVnSEpRcTlvR0wwMTlldUhSelJ6bXRtTTRnbmUxU
HpERklwdnFGTzN0eFVzNXgxZlN5"
result=base64.b64decode(code)
print result
# b'admin:202cb962ac59075b964b07152d234b70'
 $ more code.py
import base64
code = "8ejkl9wkyq6x4TZVLbAX7Reo7wp0brg6UgHJQq9oGL019euHRzRzmtmM4gne1PzDFIpvqFO3txUs5x1fSy"
result=base64.b64encode(code)
print result
# b'admin:202cb962ac59075b964b07152d234b70'
 $ python decode.py
8ejkl9wkyq6x4TZVLbAX7Reo7wp0brg6UgHJQq9oGL019euHRzRzmtmM4gne1PzDFIpvqFO3txUs5x1fSy
$ python code.py
OGVqa2w5d2t5cTZ4NFRaVkxiQVg3UmVvN3dwMGJyZzZVZ0hKUXE5b0dMMDE5ZXVIUnpSem10bU00Z25lMVB6REZJcHZxRk8zdHhVczV4MWZTeQ=
When I code I need obtain ADhlamtsOXdreXE2eAA0VFpWTGJBWDdSZW83d3AwYnJnNlVnSEpRcTlvR0wwMTlldUhSelJ6bXRtTTRnbmUxU


What am I doing wrong? Many thanks and sorry for my English!
Reply
#2
When you decode (especially if you're decoding random data), the possible output string may include null bytes. If you're using python2 and print out the data, these null bytes will just disappear.

>>> print b'\x00abc\x00def'
abcdef
>>> b'\x00abc\x00def' == 'abcdef'
False
So the printed line is not the same as the data you printed. Taking the printed line and encoding will get you something else. This isn't a problem if you only encode ascii, but if you're decoding random data, this is likely to happen.

Don't assume the printed value of the string is equal to the string. Also, use python3 and this is harder to mess up.
Reply
#3
Might be better to first encode random data, then decode the result to see if you get the original data, instead of the other way around. base64 encrypted data will always be printable ASCII characters so you will not run into the problem with null bytes and unprintable characters.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  encode/decode to show correct country letters in a CTk combobox janeik 2 660 Sep-02-2023, 09:46 AM
Last Post: janeik
Question UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 562: ord ctrldan 23 4,607 Apr-24-2023, 03:40 PM
Last Post: ctrldan
  Decode string ? JohnnyCoffee 1 788 Jan-11-2023, 12:29 AM
Last Post: bowlofred
  UnicodeEncodeError: 'ascii' codec can't encode character '\xfd' in position 14: ordin Armandito 6 2,644 Apr-29-2022, 12:36 PM
Last Post: Armandito
  'NoneType' object has no attribute 'encode' bhagyashree 6 8,737 Nov-05-2020, 03:50 PM
Last Post: deanhystad
  TypeError: ENCODE Method, str instead of byte Rajath 1 2,737 May-09-2020, 06:05 PM
Last Post: bowlofred
  struct.decode() and '\0' deanhystad 1 3,142 Apr-09-2020, 04:13 PM
Last Post: TomToad
  Getting decode error. shankar 8 10,276 Sep-20-2019, 10:05 AM
Last Post: tinman
  asyncio encode and decode, how good are they? CoderOne 2 2,292 Sep-03-2019, 11:06 PM
Last Post: wavic
  UnicodeEncodeError: 'charmap' codec can't encode character '\u2019' in position 23: c kapilan15 2 4,375 Jan-14-2019, 09:11 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020