Cannot encrypt with RSA in python 3.3

Bug #1177614 reported by Chris Lamb
6
This bug affects 1 person
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.encrypt(message)
  File "/Users/cclamb/PycharmProjects/interpreters/pyCrypto-3.3/lib/python3.3/site-packages/Crypto/Cipher/PKCS1_OAEP.py", line 150, in encrypt
    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??ڠ??s?o6???{ٍyT?B?qb?o??
                           ??fp?AC?]C?|???j? ?s??.
                                                     ?r????(?@_?6?5A[??"???6:?HX??da,q?;?"??爝F?W?8?iG
                                                                                                       ՚?? JH???u??
                                                                                                                   x??Z?t??ok?$?a???Ym??S?b?M͈w?/???? ?~T??/@?/??9?B?'???'p??m???>???.?O??ndS?~????Q,?308???y

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.encrypt(message)

print(ciphertext)

Revision history for this message
Legrandin (gooksankoo) wrote :

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.thegreenplace.net/2012/01/30/the-bytesstr-dichotomy-in-python-3/

Revision history for this message
Chris Lamb (chrislambistan) wrote : Re: [Bug 1177614] Re: Cannot encrypt with RSA in python 3.3

Ah! Thanks! We may have a documentation bug then, I followed a couple of
examples in the API docs. I will look at fixing and documenting and will
update this ticket with results.

On Tuesday, May 7, 2013, Legrandin <email address hidden> wrote:

> 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.thegreenplace.net/2012/01/30/the-bytesstr-dichotomy-in-
> python-3/
>
> --
> You received this bug notification because you are subscribed to the bug
> report.
> https://bugs.launchpad.net/bugs/1177614
>
> Title:
> Cannot encrypt with RSA in python 3.3
>
> Status in Python Cryptography Toolkit:
> New
>
> 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.encrypt(message)
> File
> "/Users/cclamb/PycharmProjects/interpreters/pyCrypto-3.3/lib/python3.3/site-packages/Crypto/Cipher/PKCS1_OAEP.py",
> line 150, in encrypt
> 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??ڠ??s?o6???{ٍyT?B?qb?o??
> ??fp?AC?]C?|???j? ?s??.
>
> ?r????(?@_?6?5A[??"???6:?HX??da,q?;?"??爝F?W?8?iG
>
> ՚?? JH???u??
>
>
> x??Z?t??ok?$?a???Ym??S?b?M͈w?/????
> ?~T??/@?/??9?B?'???'p??m???>???.?O??ndS?~????Q,?308???y
>
> 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.encrypt(message)
>
> print(ciphertext)
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/pycrypto/+bug/1177614/+subscriptions
>

Revision history for this message
Legrandin (gooksankoo) wrote :

Indeed it is a documentation bug.

FYI, I also created a fix here:

https://github.com/Legrandin/pycrypto/commit/09093410799d8e859ce570dd947b35c444931fbc

Revision history for this message
Darsey Litzenberger (dlitz) wrote :
Changed in pycrypto:
status: New → Fix Committed
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.