Migration rsa decryption with pycryptodome 3.7.2

245 views
Skip to first unread message

r.ber...@gmail.com

unread,
Dec 23, 2018, 1:25:10 PM12/23/18
to PyCryptodome
Hello,

I was using an old pyCryptodome version recently, I had the following code that worked (it's an exemple) :

from Crypto.PublicKey import RSA
rsa = RSA.importKey(open("key.der","rb").read())
result = rsa.decrypt(encryptedData)

With version pytcryptodome 3.7.2 I tried to convert it by :

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
key = RSA.importKey(open("key.der","rb").read())
rsa = PKCS1_OAEP.new(key)
result = rsa.decrypt(encryptedData)

but I get an exception ValueError: Incorrect decryption

I tried a lot of thing for several hours, but each time without success.
The encryptedData is bytes, I use python 3.7 on mac, and also on windows but I get the same error.

Probably my question is stupid, but to be honest, I do not understand the recent evolutions of pycryptodome, I just need that my old script continue to working as before.

Please, can you help me, how to replace these few lines, to make it to work.

Best regards, 

Rémi.

Helder Eijs

unread,
Dec 23, 2018, 4:17:02 PM12/23/18
to r.ber...@gmail.com, PyCryptodome
Your piece of code was valid with the old PyCrypto; PyCryptodome is a
different package that maintains most of the original APIs of
PyCrypto, but not all and one item that is not supported is indeed the
decrypt() method of an RSA object.

Such method was implementing the insecure textbook version of RSA,
which is different than OAEP encryption, as used in the second piece
of code.

Your best bet is to simply use the old PyCrypto.
Alternatively, it should still be possible to decrypt your old data
using PyCryptodome with something like this:

from Crypto.PublicKey import RSA
rsa = RSA.importKey(open("key.der","rb").read())
encryptedNumber = int.from_bytes(encryptedData, 'big')
decryptedNumber = rsa._decrypt(encryptedNumber)
decryptedData = decryptedNumber.to_bytes(rsa.size_in_bytes(), 'big')

Hope it helps...
> --
> You received this message because you are subscribed to the Google Groups "PyCryptodome" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to pycryptodome...@googlegroups.com.
> To post to this group, send email to pycryp...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

r.ber...@gmail.com

unread,
Dec 26, 2018, 2:35:31 PM12/26/18
to PyCryptodome
YESSSS !!! it works, thanks...
Reply all
Reply to author
Forward
0 new messages