0

I have a packages encoded by URL-encoded unpadded base64. It's looks like this:

b'DbMG_38MBgb8ru_RlDE1'

So I have a simple code which must turn this JSON bytes to Python object

import base64, json
b = b'DbMG_38BBgaI0Kv6kzGK'
a = base64.urlsafe_b64decode(b)
c = json.loads(a)

From b'DbMG_38BBgaI0Kv6kzGK' it should turn out this:

[
    {
        "length": 13,
        "payload": {
            "src": 819,
            "dst": 16383,
            "serial": 1,
            "dev_type": 6,
            "cmd": 6,
            "cmd_body": {
                "timestamp": 1688984021000
            }
        },
        "crc8": 138
    }
]

But I have an error:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 1: invalid start byte

Also I have tried:

c = a.decode('utf-8') # UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 1: invalid start byte
c = a.decode('utf-16') # UnicodeDecodeError: 'utf-16-le' codec can't decode byte 0x8a in position 14: truncated data
c = a.decode('ascii') # UnicodeDecodeError: 'ascii' codec can't decode byte 0xb3 in position 1: ordinal not in range(128)

But this is not working

The variable a contains:

b'\r\xb3\x06\xff\x7f\x01\x06\x06\x88\xd0\xab\xfa\x931\x8a'

Please help me decode this bytes I'm at a dead end

Nikita
  • 9
  • 1
  • 1
    those bytes are clearly not valid JSON, and clearly couldn't encode the result you expect – juanpa.arrivillaga Jul 12 '23 at 18:33
  • 2
    yeah, that's way too short – Yarin_007 Jul 12 '23 at 18:34
  • 1
    Base64 encoded strings are about 33% longer than the encoded content. A valid Base64 of your example JSON is: `WwogICAgewogICAgICAgICJsZW5ndGgiOiAxMywKICAgICAgICAicGF5bG9hZCI6IHsKICAgICAgICAgICAgInNyYyI6IDgxOSwKICAgICAgICAgICAgImRzdCI6IDE2MzgzLAogICAgICAgICAgICAic2VyaWFsIjogMSwKICAgICAgICAgICAgImRldl90eXBlIjogNiwKICAgICAgICAgICAgImNtZCI6IDYsCiAgICAgICAgICAgICJjbWRfYm9keSI6IHsKICAgICAgICAgICAgICAgICJ0aW1lc3RhbXAiOiAxNjg4OTg0MDIxMDAwCiAgICAgICAgICAgIH0KICAgICAgICB9LAogICAgICAgICJjcmM4IjogMTM4CiAgICB9Cl0=`. As you see, it's much longer and totally different from the Base64 you've shown. – jps Jul 12 '23 at 18:47
  • @juanpa.arrivillaga Calling base64.b64decode(b) results in an error: binascii.Error: Incorrect padding How then to get JSON from 'DbMG_38BBgaI0Kv6kzGK' or is it impossible? – Nikita Jul 12 '23 at 18:47
  • Your Base64 contains (whatever kind of) binary data. – jps Jul 12 '23 at 18:48
  • @Nikita that JSON is not being encoded by those b64encoded bytes. The json would require at least 148 bytes to encode as JSON. I don't know enough about compression to be able to say if possibly those bytes are compressed and could contain that JSON... but it would be something like a 7 compression ratio so I think it's unlikely to be any generic compression algorithm – juanpa.arrivillaga Jul 12 '23 at 18:54
  • @jps Okey, but server send me this short base64 and exists some shell programme which can turn this base64 to JSON, but how she does it I don't know – Nikita Jul 12 '23 at 18:57
  • @Nikita why do you think that is the case? – juanpa.arrivillaga Jul 12 '23 at 18:58
  • 1
    Then you should either use this shell program or analyze what it is doing. But based on the given information your question is not answerable. – jps Jul 12 '23 at 19:32
  • @juanpa.arrivillaga Okey, if it is not JSON, so how I can decode b'\r\xb3\x06\xff\x7f\x01\x06\x06\x88\xd0\xab\xfa\x931\x8a' if I don't know what encoding is using ? – Nikita Jul 12 '23 at 19:36
  • 1
    @Nikita **why do you think this is even possible*?? How are we supposed to *guess* this? I suspect you are *mistaken* about what these bytes are supposed to represent. – juanpa.arrivillaga Jul 12 '23 at 22:31
  • @juanpa.arrivillaga These bytes really encode json, but it was all about the encoding that was used before using base64, I figured out how to use the encoding I needed, manually wrote the encoder and decoder, and I succeeded. Thank you for your time – Nikita Jul 14 '23 at 22:43

0 Answers0