1

let say i got string(that is supposed to be a bytes):

"b'YzJGNVltYzJGNVltOTBlbk5oZVdKdmRIcHpZWGxpYjNSNmMyRjVZbTkwZW5OaGVXSnZkSHB6WVhsaWIzUjZjMkY1WW05MGVnOTBlblJsYzNRPXNheWJvdHo='"

and i want to decode it but it say it got to be in bytes so i got to turn that to this somehow

b'YzJGNVltYzJGNVltOTBlbk5oZVdKdmRIcHpZWGxpYjNSNmMyRjVZbTkwZW5OaGVXSnZkSHB6WVhsaWIzUjZjMkY1WW05MGVnOTBlblJsYzNRPXNheWJvdHo='

i am working on a chat client and when it sent the data(bytes), it turn into a string somehow when passing though the server and when it reach the other side, it can't decode it

ps: it is ENCRYPT, decoding that will give random number and letters, the client will decrypt it

oh, and, i don't really know if the '=' sign is supposed to be there, when i got the data in a test between me and my friend, i got it like that string(the one on the top) with the error saying that it got to be in byte.

part of the code in the client(just one line to show everyone, the rest is a secret):

base64.b64decode(that_string).decode('ascii')

it mainly use ascii so i think this is right, right?

more info:

base64.b64encode(message.encode('ascii'))

the message here is for getting the string sent from the other side

Room.message(str(secretEncrypt(par, codes())))

i don't know how i miss this, it str it before it send =.= well, it still need to turn it to bytes, how so i do that?

7
  • str.encode() in python3.x uses utf-8 by default docs.python.org/3/library/stdtypes.html#str.encode Commented Feb 3, 2013 at 21:10
  • It looks like your friend sent you repr(data) instead of just data. Show the code doing the sending and we can fix the right problem. Commented Feb 3, 2013 at 21:27
  • base64.b64encode(...) returns bytes, so how is it sent to the client? Commented Feb 3, 2013 at 21:32
  • edited, turn out it str it before senting it... Commented Feb 3, 2013 at 21:40
  • It sounds like what you really want to do is smuggle an arbitrary bytes through a str without decoding/encoding (in other words, send each byte as the Unicode codepoint with the same number). This isn't impossible—but it's almost always a sign that you're doing something wrong. There's almost always a better answer. For example: If your API is byte-based, use bytes instead of str in the API; if your bytes are ASCII (which is always true for base64), just b.decode('ascii') to get the str; etc. Commented Feb 3, 2013 at 22:51

3 Answers 3

3

The following greatly works

>>> import base64
>>> s = "Hello StackOwerflow!"
>>> b = s.encode()
>>> c = base64.b64encode(b)
>>> c
b'SGVsbG8gU3RhY2tPd2VyZmxvdyE='
>>> b1 = base64.b64decode(c) 
>>> b1
b'Hello StackOwerflow!'
>>> b1.decode()
'Hello StackOwerflow!'

Show how you encode string

Sign up to request clarification or add additional context in comments.

6 Comments

and i found out that i didn't give a lot of info... sorry... i am new here and didn't really want to give a lot of the codes till it is done
oh, and that didn't really help, it already got a b'the-codez in the string... and having a b"b'the-codez will not work
We can't help you, until you show, how you encrypt and decrypt your strings
If you want secure transfer, you should use RSA or AES, not base64. Use dlitz.net/software/pycrypto
naw, no need something like that, it just a chat client, after it done, it be use to sent secret message without the other team knowing when play a chatroom game or for senting links without the admin of the room knowing, after all, it hidden and only my chat client can decrypt it and see it, and even if they see it, they will just see random number and letter
|
3

The problem is here:

Room.message(str(secretEncrypt(par, codes())))

secretEncrypt(...) returns bytes, but then you do str(b"thereturnvalue") which does not do what you want:

>>> my_bytes = b"abc"
>>> type(my_bytes)
<class 'bytes'>
>>> str(my_bytes)
"b'abc'"
>>> type(str(my_bytes))
<class 'str'>

It is putting the string-representation (repr) of your bytes into a string, which is why your string starts with b"

How best to fix this depends on the surrounding code.. Either:

  • if the secretEncrypt function returns bytes, make Room.message accept bytes also (and remove the str(...) cast).
  • Make secretEncrypt return a string, removing the need for the str(...) cast
  • Least elegant solution: decode the bytes returned from secretEncrypt, turning it into a string. Something like this:

    encrypted = secretEncrypt(par, codes())
    Room.message(encrypted.decode("whatever-encoder-was-used"))
    

    Where whatever-encoding-was-used is the text encoding (likely utf-8 or ascii, but it is impossible to tell without seeing the rest of your code)

3 Comments

the Room.message is very messy already, i don't think i want to mess with it because it the main code, while secretEncrypt is a lot smaller and clearer, so, how would i make it remove the str(...) cast?
@freeforalltousez By making secretEncrypt return a string, so you can remove the str() call. I've added another solution (decoding the the bytes returned by secretEncrypt, which can then be passed to Room.message)
oops, forgot to mark the answer that i post, it less messy and easier for new comers to understand, thanks anyway.
0

and i was right, no need to over think it

the string:

"b'YzJGNVltYzJGNVltOTBlbk5oZVdKdmRIcHpZWGxpYjNSNmMyRjVZbTkwZW5OaGVXSnZkSHB6WVhsaWIzUjZjMkY1WW05MGVnOTBlblJsYzNRPXNheWJvdHo='"

just need to be split, eg:

string = "b'YzJGNVltYzJGNVltOTBlbk5oZVdKdmRIcHpZWGxpYjNSNmMyRjVZbTkwZW5OaGVXSnZkSHB6WVhsaWIzUjZjMkY1WW05MGVnOTBlblJsYzNRPXNheWJvdHo='"
string = string.split("'")[1]

and that would get me a string without the b':

"YzJGNVltYzJGNVltOTBlbk5oZVdKdmRIcHpZWGxpYjNSNmMyRjVZbTkwZW5OaGVXSnZkSHB6WVhsaWIzUjZjMkY1WW05MGVnOTBlblJsYzNRPXNheWJvdHo="

then i just need to bytes it:

string = bytes(string, "ascii")

which would get me the b' outside the string:

b"YzJGNVltYzJGNVltOTBlbk5oZVdKdmRIcHpZWGxpYjNSNmMyRjVZbTkwZW5OaGVXSnZkSHB6WVhsaWIzUjZjMkY1WW05MGVnOTBlblJsYzNRPXNheWJvdHo="

ps: to everyone out there who say things about cracking my code, it got randoms mumbo jumbo things with the id dump into it, if you manage to crack it, it not much use and anyway, it just a chat client for a mini chatroom games for kids

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.