Reputation: 11
New rephrased Question
There are two programs that work together, a client and a server.
The client is having issues decrypting, and i have ran the following test on the client without any server interaction and this does not work.
I get rsa.pkcs1.DecryptionError: Decryption failed
when i run this code on the client.
# Public key saved in ini file as this format "PublicKey(n, e)"
# Private key saved in ini file as this format "PrivateKey(n, e, d, p, q)"
key_string = public_key.strip("PublicKey(").strip(")")
n, e = key_string.split(", ", 1)
value = rsa.encrypt(b"Hello", public_key)
key_string = self.private_key.strip("PrivateKey(").strip(")")
n, e, d, p, q = key_string.split(", ", 4)
private_key = rsa.PrivateKey(int(n), int(e), int(d), int(p), int(q))
decrypted = rsa.decrypt(value, private_key)
Old "Question" asked
I am writing a python program that is essentially a P2P chat application utilising a rendezvous server for new connections.
Walkthrough of the steps taken by client/server.
Client:
- Connects to server using sockets
- Sends its public key to server
Server:
- Reads public key
- Creates AES key and ciphers a message (list of already connected peers)
- Encrypts the AES Key using the clients RSA public key
- Sends the key and ciphertext
Client:
- Reads the information and splits into the key portion and the ciphertext portion
- Decrypts the AES Key (However this fails even though the same code works on the server to decode)
- Decrypts the cipher text using the now unencrypted AES Key
# Encrypt with AES cipher_text, key, nonce = self.aes.encrypt(json.dumps(message)) # Encrypt AES Key with RSA encrypted_key = self.rsa.encrypt(key, peer['public_key']) # Send data to peer self.socket.sendto(encrypted_key + nonce + cipher_text, peer['address']) ``` ``` CLIENT CODE data, address = self.socket.recvfrom(65536) recv = {"key": data[:256], "nonce": data[256:272], "data": data[272:]} key = self.rsa.decrypt(recv["key"]) peers = json.loads(self.aes.decrypt(recv["data"], key, recv["nonce"])) ```
Upvotes: 0
Views: 1378
Reputation: 11
Solved this, Thanks for the help!
The error was with my import of the config file, my statement was checking if there was a valid RSA-pub/priv key and if there wasn't it would generate a new pair for the user.
The problem was it was always generating a new keypair. meaning it was attempting to decrypt with the incorrect private key.
Upvotes: 1