Cannot encrypt with RSA in python 3.3
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
Python-Crypto |
Fix Committed
|
Undecided
|
Unassigned |
Bug Description
Hi everyone, I can't encrypt a message using RSA in python 3.3. Python 3.3 virtualenv with pyCrypto:
(pyCrypto-3.3) s946259:pyCrypto cclamb$ python test.py
Traceback (most recent call last):
File "test.py", line 9, in <module>
ciphertext = cipher.
File "/Users/
db = lHash + ps + bchr(0x01) + message
TypeError: can't concat bytes to str
Python 2.7 virtualenv with pyCrypto:
(pyCrypto-2.7) s946259:pyCrypto cclamb$ python test.py
??םB??;?
??q??ڠ?
Here's the contents of test.py:
from Crypto.PublicKey import RSA
from Crypto import Random
from Crypto.Cipher import PKCS1_OAEP
rng = Random.new().read
message = 'To be encrypted'
key = RSA.generate(2048, rng)
cipher = PKCS1_OAEP.new(key)
ciphertext = cipher.
print(ciphertext)
The line:
message = 'To be encrypted'
should be:
message = b'To be encrypted'
In Python 3, strings are not encoded and therefore cannot be encrypted (unless one silently performs some arbitrary encoding like Latin 1). In Python 2, strings and encoded strings (that is, a "bytes" object) were mixed and your example would have worked.
See here:
http:// eli.thegreenpla ce.net/ 2012/01/ 30/the- bytesstr- dichotomy- in-python- 3/