0

I have base64 encoded string sZCLmg== which is Note. What I am trying to do is to decode it with base64 and then use bytes negotiation to get string Note back.

import base64 encoded = 'sZCLmg==' #sZCLmg== Note data = base64.b64decode(encoded) print data mylist = [] mylist.append(data) #print mylist[0][0] bytes = mylist[0][0] print (bytes ^ 0xFF) 

but I am getting error: ValueError: invalid literal for int() with base 10: '\xb1' Any idea please what am I doing wrong to get original string Note?

2
  • 1
    Are you sure that's the correct encoded string? I'm getting Tm90ZQ== for Note Commented May 26, 2019 at 16:13
  • 1
    @TheGamer007, apparently the bytes were negated before Base64 encoding. Commented May 31, 2019 at 0:01

1 Answer 1

2

In Python2, iterating over the literal '\xb1\x90\x8b\x9a' produces strings, not bytes.

One solution would be to use a bytearray.

>>> ba = bytearray(data) >>> ''.join(chr(x ^ 0xFF) for x in ba) 'Note' 

As @wovano points out in the comments, also it's possible to do this without using a bytearray, like this:

''.join(chr(ord(x) ^ 0xFF) for x in data) 
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, it results in strings, which cannot be xor'ed with 0xFF. But using bytearray is not necessary. The following gives the same result: ''.join(chr(ord(x) ^ 0xFF) for x in data).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.