flipflop
flipflop

Reputation: 45

out of box example does not work on python 3.3 expected bytes str found

from rsa import *

def main():
    (bob_pub,bob_pri)=newkeys(512)
    message ='Number of bits needed to represent a integer'
    crypto = encrypt(message,bob_pub)
    message1 = decrypt(crypto,bob_pri)
    print(message1)

if __name__ == "__main__" :
    main()    

hi everyone im new to site this is my first post

ive installed python 3.3 here is a pip list of my installation

C:\Python33>pip list
beautifulsoup4 (4.3.2)
mechanize (0.2.5)
mpmath (0.18)
pip (1.5)
pyasn1 (0.1.7)
pyLibrary (0.1.13316)
PyMySQL (0.6.1)
requests (2.2.0)
rsa (3.1.2)
setuptools (2.1)

C:\Python33>

The rsa documentation for an example program is the same as the code sample but I get the following error message

Traceback (most recent call last):
  File "C:\Python33\my code\rsa hacker.py", line 11, in <module>
    main()
  File "C:\Python33\my code\rsa hacker.py", line 6, in main
    crypto = encrypt(message,bob_pub)
  File "C:\Python33\lib\site-packages\rsa\pkcs1.py", line 166, in encrypt
    padded = _pad_for_encryption(message, keylength)
  File "C:\Python33\lib\site-packages\rsa\pkcs1.py", line 106, in _pad_for_encryption
    message])
TypeError: sequence item 3: expected bytes, str found

this code should work I have checked it is 3.3 compatible and my installation seems fine im not sure where to go from this. do try another rsa package do change to 2.7 or is it an installation problem please could someone explain in very basic steps

Upvotes: 1

Views: 3027

Answers (1)

9000
9000

Reputation: 40894

Your message is actually a string (str). It happens to be an ASCII string and thus could look to you as a byte string. It is not; it could be 'Привет 世界' as well. Representation of strings is a complex matter; there's no single natural characters-to-bytes transform.

What you need is message.encode('utf-8') (or choose another encoding). This is your strings encoded as a UTF-8 array of bytes which the encoder expects.

After decoding, you will need message1.decode('urf-8') to get the original test back.

Upvotes: 4

Related Questions