2
votes

I can decrypt some data encrypted via openssl command line tool, but some 'extra' data is returned with the original data. I've created a encrypted file like this:

$ echo this is it >file.txt

$ openssl rsautl -encrypt -pubin -inkey public.pem -in file.txt -out encrypted.txt

And I can access the original data with:

from Crypto.PublicKey import RSA
key = open('/tmp/.private.key').read()
rsakey = RSA.importKey(key, 'MyPassphrase')
data = open('/tmp/encrypted.txt', 'rb').read()
original = rsakey.decrypt(data)

But some extra data is returned and the output is something like this:

\x02h\x83\xcfx\x84,\xb1\xa6 [...] \xcf5\x9f\xbbG\xf1\x14\xd0\x8d\x1f\xfe\x9c4\xbb\x1aB\xfa\xc3b\xc2\xe0K\x85\xb5\x10y\xe1\x8e\x00this is this\n

Can I avoid to receive this raw data before the decrypted data?

Obs.: The keys were created with openssl tool

Tks.

1
How exactly do you output original? - Alik
if ai print(original) it shows some binary with the data in the end: -VM-'~C M-fM-\ ~?~LPOZw^O^S^[M--M-:^WM-4M-,&IM-i^W6 ^\~I )M-B"^^_ ~_M-;G^TЍ^_M-~~\4M-;^ZBM-CbM-`K~EM-5^Py his is it I can do this to get the data: rsakey.decrypt(data).split('\x00')[1] But I.m not sure if is the right way. - Rafael

1 Answers

3
votes

You are getting PKCS#1 v1.5 padded plaintext back. You need to remove the PKCS#1 v1.5 padding first. Currently you are performing textbook (or "raw") RSA decryption, which is little more than modular exponentiation. Try a PKCS#1 v1.5 capable class instead, like this one