Two terminals, one TCP connection, and a deliberately bad first impression: before key sharing, the words are readable on the wire.
That is the point of this coursework project. Instead of treating “encrypted” as a badge in the corner of a UI, the protocol makes its transition inspectable. /sendpub exchanges public keys; /share starts a key-establishment handshake; later messages use AES-GCM. The source remains a learning implementation, not a replacement for a mature secure messenger.
Start with something you can read
The original capture contains a newline-delimited JSON message with type: PLAIN_CHAT. The text says “Hello, This is test Text.” There is no puzzle for Wireshark to solve: the sentence is already in the payload.

After the handshake, the capture changes to SEC_CHAT. The outer envelope is still readable JSON, but the chat body is represented by a Base64-encoded encrypted blob.

Base64 is not the encryption. It is a text representation used to carry binary bytes inside JSON. A compressed file can look opaque too; unreadability by eye is not a cryptographic test.
The screenshots preserve an earlier demonstration. The implementation discussion below follows the inspected source revision, including its later receive-policy checks.
Four properties, not one adjective
| Property | The question | Mechanism in this implementation |
|---|---|---|
| Confidentiality | Can someone without the session key read the protected body? | AES-256-GCM encryption |
| Integrity | Was the protected body or its associated context changed? | GCM authentication tag and associated data |
| Key possession | Does the handshake signature verify under the supplied peer key? | RSA-PSS over a defined payload |
| Freshness and ordering | Does this belong to the current exchange and expected message sequence? | Handshake nonces, handshake time checks and per-session chat sequence numbers |
The deliberately narrow wording of the third row matters. Verifying a signature under a key does not establish whose key it is. That needs a trust decision outside the signature calculation.
Why RSA carries a key, not the conversation
The code generates RSA-2048 key pairs. Its OAEP helper explicitly selects SHA-256, while RSA-PSS signs a SHA-256 digest. A fresh 32-byte random session key becomes the AES-256 key.
There is a practical reason for this hybrid design. With a 2,048-bit RSA modulus and SHA-256, OAEP can encrypt at most 256 − 2 × 32 − 2 = 190 bytes. A 32-byte session key fits; an arbitrary chat history does not.
The library’s OAEP documentation also makes an important distinction: encryption to a public key does not authenticate who created the ciphertext. Anyone with that public key can encrypt to it. The signature has a separate job.
RSA and AES key lengths are not directly comparable measures of strength. “2,048 versus 256” is not a scoreboard; they are different constructions solving different parts of the exchange.
The handshake has two local state machines
Public keys are exchanged first. The inspected handshake then proceeds as follows:
- The initiator sends
KEY_REQwith a random nonce, A. - The responder returns
KEY_CHALLENGE, echoing A and adding nonce B. - The initiator generates the session key, encrypts it to the peer’s RSA public key, and sends a signed
KEY_SET. - The responder checks the pending nonces, timestamp, signature, decrypted key length and derived key ID. It activates the session locally and sends an AES-GCM-protected acknowledgement.
- The initiator verifies the acknowledgement’s context, nonce B, key ID and freshness before activating its own session.
The peers therefore do not enter the established state at the same instruction. The acknowledgement is part of the protocol, not decoration added after “the encryption worked.”
The signed KEY_SET payload contains the sender label, timestamp, both nonces, key ID and encrypted key. Both sides serialize that selected payload with sorted JSON keys and compact separators before signing or verifying it. A signature needs an agreed byte representation, not two objects that merely look equivalent on screen.
The key ID is the first 16 hexadecimal characters of SHA-256 over the session key. It selects a session; it is not the key, a signature or a replacement for the GCM tag.
Inside the encrypted blob
crypto.py returns a specific binary layout: 12 bytes of nonce, 16 bytes of tag, then ciphertext. The plaintext being encrypted is the serialized inner JSON, containing sender, timestamp, sequence and text—not just the visible sentence.
There are 28 bytes of nonce-and-tag overhead before Base64. If the serialized inner message contains n bytes, this binary blob contains n + 28; its Base64 form occupies 4 × ceil((n + 28) / 3) characters before the outer envelope is added.
Associated data, or AAD, binds the purpose and key ID. The implementation uses a context string such as P2P-CHAT-V2|SEC_CHAT|key_id, and a different purpose for KEY_ACK. That context is authenticated, not encrypted. Supplying the wrong context makes tag verification fail.
A local smoke run of the actual helpers recovered the original message, rejected a changed ciphertext byte, and rejected the same blob under the wrong purpose. It also exercised OAEP key recovery and PSS verification. Those are checks of the primitives and their use here, not proof of the entire protocol against every attacker.
GCM nonces must not repeat under a key. This implementation draws 12 random bytes for each encryption. Random generation is not a mathematical guarantee of uniqueness for an unlimited number of messages; key usage and collision risk still matter.
“Verified” is not a trust source
The peer sends a public key and a claimed fingerprint. The receiver computes a SHA-256 fingerprint from the PEM text and stores the received key. If the key changes, the displayed verification state is reset.
What establishes that key’s connection to a person? Not the key’s own claim.
The UI provides /verify, optionally with an expected fingerprint. With no argument, the inspected implementation simply marks the currently received fingerprint as acknowledged. With an argument, it compares against the supplied value. A meaningful comparison needs an independently trusted reference, not the same untrusted connection supplying both the key and its supposed identity.
The handshake is not gated on that verification flag. Consequently, the correct claim is not “RSA proves it is really them.” It is that the signatures verify possession of the private key corresponding to the public key the application has accepted.
This is the same separation that matters in Passless: cryptographic validity and account or identity authorization are not interchangeable.
Time, sequence and replay are different checks
protocol.py defines a ±180-second freshness window. The inspected code applies time checks during the key handshake and binds its pending nonces to that exchange.
For encrypted chat, the receive handler checks the decrypted timestamp’s type, but does not apply that age window. Instead, it requires a positive sequence number equal to the next expected value. Both counters reset when a session is activated.
A local receive-path check confirmed the distinction: an authenticated message with an old timestamp and the expected sequence was accepted; a duplicate sequence and a skipped sequence were refused. So “timestamp freshness protects every chat message” would overstate the implementation.
This is not just vocabulary. Age asks how long ago something happened. Sequence asks where it belongs in a session. A new message can arrive late; an old message can still be correctly ordered.
The plaintext boundary is a policy
The later receive path warns when plaintext arrives after a secure session has been established. In strict mode, it drops that post-session plaintext instead of displaying it. Sending is also blocked before a secure session when strict mode is enabled.
But the current implementation does not refuse incoming plaintext in every state:
| Local state | Sending | Incoming plaintext |
|---|---|---|
| No session; strict off | Plaintext | Displayed |
| No session; strict on | Refused | Still displayed in the inspected code |
| Session established; strict off | Encrypted | Warned about, then displayed |
| Session established; strict on | Encrypted | Warned about and dropped |
The existing four pytest checks pass. A separate local receive-path smoke also checked the missing pre-session strict case. This page records the narrower behaviour; the application source remains unchanged during this website pass.
Encrypting the message does not decide when plaintext is acceptable.
What the capture cannot prove
The outer JSON still exposes message type, sender label, time, key ID and encoded length. IP addresses, ports, traffic timing and connection duration remain visible too. Content encryption is not traffic anonymity.
Rekeying generates a new session key through the same exchange and resets session counters. It does not introduce an ephemeral Diffie–Hellman exchange or establish forward secrecy. If the RSA private key used to receive a recorded session-key envelope is later compromised, rotating AES keys through that same RSA key does not make those recordings safe.
TCP also gives a byte stream, not one application message per network packet. The receive loop accumulates bytes and extracts complete newline-delimited envelopes. One recv(4096) can return part of a message, several messages, or boundaries that have nothing to do with the JSON lines.
These details make the exercise useful to me: they are places where an attractive diagram or a green “secure” label can outrun the code. The packet capture starts the question; it does not finish the security argument.
Follow the layers
Read crypto.py for primitives and byte layout, protocol.py for serialization and the freshness helper, then the actual handlers in main.py to see which checks are applied in each state.
NIST SP 800-38D specifies GCM and its associated-data model. PyCryptodome’s OAEP documentation explains the size limit and why public-key encryption needs a separate authentication argument. For real private communications, I would choose an established authenticated protocol rather than promote this coursework into a secure-messaging product.
