I have these sequence of commands through the shell that work fine:
fishgills@fishgills-Mac ~ cat /tmp/secret_message
Helloo there.%
fishgills@fishgills-Mac ~ openssl rsautl -encrypt -inkey ~/Documents/test_pub.pem -pubin -in /tmp/secret_message -out /tmp/test.enc
fishgills@fishgills-Mac ~ openssl rsautl -decrypt -inkey ~/Documents/test.pem -in /tmp/test.enc
Helloo there.%
fishgills@fishgills-Mac ~
As you can see, everything works fine. Encrypt the message and is able to decrypt it.
So in python, I'm trying to do the same:
from Crypto.PublicKey import RSA
rsakey = RSA.importKey(open("/Users/fishgills/Documents/test.pem", 'rb').read())
raw_data = open("/tmp/test.enc", 'rb').read()
decrypted = rsakey.decrypt(raw_data)
print decrypted
And the program's output is:
fishgills@fishgills-Mac ~ python main.py ~/Documents/test.pem
�%��u�9��炿��:og:_�Z��WF/��W �v��������a�Jw+�J�Th�N9`V���5t##Go0�
#��A2e*����a�����a�ň��ߘhQX��U�7AB��B�Q�X�l�� ����rkK����� �kKj��\u���PILT�@���Rj���0:��߰9˕�Helloo there.
You can see the message in there... but there's a pile of junk. PyCrypto documentation says that the decrypt method returns a byte string but I haven't been able to decode it properly, I get things like:
Traceback (most recent call last):
File "main.py", line 9, in <module>
print decrypted.decode("utf-8")
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xd7 in position 1: invalid continuation byte
Anyone able to point me in the right direction?