Reputation: 35
I've got a problem. I'm using pycrypto and rsa. I want to generate my rsa keys. Then I want to send my public key (in binary or base64 or similar) but first I want to encrypt it with server public. Because I want to be sure that no one is sniffing and my public needs to be not well known.
And then the problem starts. Because when I'm encrypting my public key, after decrypting it, decrypted data is unreadable.
And I have no idea why. When I exchange publics and send normal data (not public keys) encrypting works. I can't find sollution to how send my public encrypted.
Can someone help me? Every comment will be useful
My code:
random_generator = Random.new().read
self.private_key = RSA.generate(1024, random_generator)
self.public_key = self.private_key.publickey()
keytoexport =self.public_key.exportKey(format='PEM', passphrase=None)
#client encrypting to server
def _encrypt(self, content):
return self.server_public_key.encrypt(content, 32)
#server decrypting content
def _decrypt(self, content):
return self.parent.private_key.decrypt(content)
Im sending datagram by client like that.
def send_datagram(self, datagram):
datagram = pickle.dumps(datagram)
self.socket.sendall(datagram)
response_server = self.socket.recv(2048)
return pickle.loads(response_server)
And retriving it to server like that.
receive_socket = self.request
ask = receive_socket.recv(2048).strip()
Upvotes: 0
Views: 1092
Reputation: 93958
The public key of a key pair is by definition longer than the modulus, as it contains the modulus. The modulus will be 128 bytes as it defines the key size for RSA. So by definition you cannot encrypt an RSA key with a key of the same size.
So you can do things:
You need to consider padding oracle attacks for both RSA encryption and for AES if you choose to implement this scheme. So use OAEP/AES-GCM or OAEP/AES-CBC/HMAC as encryption schemes or you might as well post your public key here.
Upvotes: 1