0

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"])) ```
10
  • What kind of error/exception does it throw? Can you provide some more info? It might be you're badly receiving/sending the encrypted AES key via the socket, since server-side it works. Commented Jul 27, 2022 at 14:03
  • packages\rsa\pkcs1.py", line 282, in decrypt raise DecryptionError("Decryption failed") rsa.pkcs1.DecryptionError: Decryption failed Commented Jul 27, 2022 at 14:05
  • Looking into that error this is where it is breaking. pkcs1.py: # If we can't find the cleartext marker, decryption failed. cleartext_marker_bad = not compare_digest(cleartext[:2], b"\x00\x02") Commented Jul 27, 2022 at 14:07
  • Are you sure you're slicing the data stored in the dictionary correctly, client side? Commented Jul 27, 2022 at 14:07
  • I've printed it out along with the type on each end, both being identical Commented Jul 27, 2022 at 14:09

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.