Question

Using PyCrypto RSA

import base64
from Crypto.PublicKey import RSA

key = RSA.importKey(open('./keyBR.pub', 'r').read())
privkey = RSA.importKey(open('./privkeyBR.pem', 'r').read())

>>> message = "This is the story"
>>> ciphertext = key.encrypt( message, None )
>>> print ciphertext
("\xd9\x97o\x85#\xc8\xba\x14\xc9\xa4h\xacg\xc8\xc7\xb3\xbfA\xf1\x8e\x8aI\xe72IT\x03\x1a\xb7\x19\xd4\xb0\x9f\x9a\xde\x13-m\x9dw\x9d\\\x8f\x9fG\xb8(\x91\xa3\xcd\xad\xf6\xea(\x16\xd5]\xeb\x96\xc5\xe3\xecy\x9b\xf9o\x0b\\P3b\xac\xcf\xcf#\xe5\x9e\x07\xe0&\x1f8\x145!\xc8\xc9\xe0x\x7f\xa1\xbf\x03\xda\xa8z]\xa3\xff\xa7\xf0\xa7\t\xef\x80An\xd8i\xd9\xb8_1\xb7\x02\x1e\xa3\xb4z\x99c\x1d\x85\xef#b\xe8a\x18\xd3\xe09\x7f\xb6\x91!\x0b\x06\x07\xb0\xd3\xa3\x7f\xc5\x97\xa1\x90\x94\xa7M\xd8\xf2(\xe3\xe3\xc2\x1es\xba\xae\x0c\xb7\xa6\xe1\xd2\xb03\xc1\xcaU\xca@\xd1\x81\xde@\xa0\t\xf1<c\x9e\x8a\x8b\x88Lj\x16 lc\xae<\xbf\x16R\xe8\x04}\xcd\x7f\x0bZA\xd9y\x14\x03\xb7Hg\xac\xf4\xa5\xce\xe0\xa2\x8b\xdai\x03\xbb5\x99XS\xfe/\xc7I\xd3D\x81'xZ\xed\xce\t,E\xfc\x90\x1a_\xb3\xa7s\x0coA&1/\x06 ",)

privkey.decrypt(ciphertext[0]) 'This is the story'

What is the format of ciphertext[0]? I thought it was utf-8 but it fails when I try.

I am using Debian 64bits, python 2.7, just in case.

Was it helpful?

Solution

It's encrypted text. It's just a stream of bytes.

If you want to send it as text, use base64 or something similar.


And one more thing. It is not directly related to your question, but it is very important. You are using RSA in a very wrong way. Your code is totally insecure.

The right thing to do is generate a fixed-length random key and use it for symmetric encryption (I'd suggest AES in CTR mode) of your data. Then you should use PKCS#1-OAEP at the beginning of your session only to establish this key.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top