Skip to content

Latest commit

ย 

History

8 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ” SecureS2S

RSA + AES-GCM Encrypted Server-to-Server Communication System

Python RSA AES Sockets Cryptography License

A secure peer-to-peer server communication system implementing a hybrid cryptographic architecture using RSA-2048 for secure key exchange and AES-256-GCM for authenticated encrypted messaging, carried over a length-prefixed framing layer so TCP's stream semantics can never desynchronize the protocol.

The project demonstrates how modern secure communication protocols such as TLS, HTTPS, and SSH establish secure channels by combining asymmetric and symmetric cryptography and how real message-oriented protocols have to solve the TCP framing problem to do it reliably.


๐ŸŽฅ Demo

Architecture Screenshot

SecureS2S Architecture

Demonstration Video

Watch the project demonstration video here:

https://drive.google.com/drive/folders/1Nj8TFVBfTfe7szLYRo91aSQZ5-aqncpW?usp=sharing

๐Ÿ“– Case Study

Case Study

A comprehensive technical article explaining the architecture, cryptographic workflow, framing protocol, implementation details, security trade-offs, and future improvements.


๐Ÿ“‘ Table of Contents


๐ŸŒŸ Features

๐Ÿ”‘ RSA Secure Key Exchange

  • Generates a 2048-bit RSA key pair.
  • Uses RSA-OAEP with SHA-256.
  • Securely exchanges the AES session key.
  • Prevents exposure of symmetric keys over the network.

๐Ÿ”’ AES-256-GCM Authenticated Encryption

  • Encrypts all communication using AES-256-GCM.
  • Provides confidentiality, integrity, and authenticity.
  • Automatically validates message integrity during decryption.
  • Drops corrupt or tampered payloads without crashing the receive loop.

๐ŸŽฒ Random Nonce Generation

  • Generates a fresh 96-bit nonce for every encrypted message.
  • Prevents nonce reuse attacks.
  • Follows NIST recommendations for AES-GCM.

๐Ÿ“ฆ Length-Prefixed Message Framing

  • Every payload the RSA public key, the wrapped session key, and every encrypted message is sent with a 4-byte big-endian length header.
  • recv_exact() blocks until the full declared payload has arrived, eliminating TCP short reads.
  • Prevents the handshake or the encrypted stream from ever desynchronizing due to partial or coalesced recv() calls.

๐Ÿ”„ Full Duplex Communication

  • Supports simultaneous sending and receiving.
  • Uses multithreading for bidirectional communication.
  • Two threading.Event flags (key_established, send_ready) coordinate outgoing messages and ACKs so neither thread blocks on a socket that isn't ready yet.
  • Mimics real-world secure communication systems.

๐Ÿ“จ Automatic Delivery Acknowledgements

Messages automatically generate encrypted acknowledgements, gated by a 5-second readiness timeout so the receive thread can never hang indefinitely on a not-yet-connected outbound socket.

Example:

> Hello Server B
(Arrived)

This confirms that the message was successfully decrypted and received by the remote server.


๐Ÿ“ก Wireshark Verifiable Encryption

Network traffic can be inspected using Wireshark.

Captured packets contain only encrypted binary payloads and no plaintext application data.

Example filter:

tcp.port == 65431 || tcp.port == 65432

๐Ÿ›ก Message Integrity Protection

AES-GCM automatically detects:

  • Modified packets
  • Bit-flipping attacks
  • Message tampering
  • Invalid authentication tags

โšก Lightweight and Minimal Dependencies

Requires only:

  • Python standard library
  • Cryptography package

No external brokers or frameworks are required.


๐Ÿ— Architecture

                    RSA KEY EXCHANGE (length-prefixed)
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                                             โ”‚
โ”‚  Server A generates RSA key pair            โ”‚
โ”‚                                             โ”‚
โ”‚  Server A โ”€โ”€ send_framed(Public Key) โ”€โ”€โ–บ B  โ”‚
โ”‚                                             โ”‚
โ”‚  Server B generates AES-256 key             โ”‚
โ”‚                                             โ”‚
โ”‚  Server B โ”€โ”€ send_framed(RSA(AES Key)) โ–บ A  โ”‚
โ”‚                                             โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

                 SECURE CHANNEL ESTABLISHED

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                                             โ”‚
โ”‚   AES-256-GCM Encrypted, Length-Prefixed    โ”‚
โ”‚              Communication                  โ”‚
โ”‚                                             โ”‚
โ”‚      Server A โ—„โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ–บ Server B โ”‚
โ”‚                                             โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Every arrow above is a send_framed()/recv_framed() call, not a raw sendall()/recv() the 4-byte length header is what lets each side reconstruct exact message boundaries regardless of how the TCP stack chunks the underlying bytes.


โš™๏ธ Installation

Clone Repository

git clone https://github.com/usfa7med/SecureS2S.git
cd SecureS2S

Install Dependencies

pip install -r requirements.txt

๐Ÿš€ Usage

Configure IP Addresses

Inside:

server_A.py
server_B.py

Set:

REMOTE_HOST = "192.168.x.x"

to the actual IP address of the peer machine on your network. (The checked-in files use a placeholder, HOST_IP_ADDRESS, so the real address is never committed to the repo replace it with a literal string or load it from an environment variable before running.)


Start Server A

python server_A.py

Start Server B

python server_B.py

Begin Messaging

Example:

> Hello
> This message is encrypted.
> Hybrid cryptography is awesome.

๐Ÿ“‚ Project Structure

SecureS2S/
โ”‚
โ”œโ”€โ”€ assets/
โ”‚   โ””โ”€โ”€ photo.png
โ”‚
โ”œโ”€โ”€ crypto.py      
โ”œโ”€โ”€ server_A.py
โ”œโ”€โ”€ server_B.py
โ”‚
โ”œโ”€โ”€ LICENSE
โ”œโ”€โ”€ README.md
โ””โ”€โ”€ requirements.txt

๐Ÿ” Security Analysis

Security Property Status
Confidentiality โœ…
Integrity โœ…
Authentication โœ…
Message Boundary Integrity โœ…
Replay Protection โš  Partial
Forward Secrecy โŒ
Perfect Forward Secrecy โŒ

โš  Challenges & Solutions

Problem:

Transmitting AES keys directly over the network would expose them to interception.

Solution:

Used RSA-OAEP encryption to securely exchange the AES session key.


Problem:

AES-CBC encryption does not guarantee message integrity.

Solution:

Implemented AES-GCM authenticated encryption.


Problem:

Static IV reuse could compromise encrypted traffic.

Solution:

Generated a new random nonce for every encrypted message.


Problem:

TCP is a byte stream, not a message protocol a single recv(4096) call could return a partial message, a full message, or several messages concatenated together, silently corrupting the handshake or breaking AES-GCM decryption.

Solution:

Added a length-prefixed framing layer (send_framed/recv_framed/recv_exact) so every payload is read exactly once, byte-for-byte, regardless of how the kernel chunks the underlying TCP stream.


Problem:

The receive thread could send an encrypted ACK before the outbound socket to the peer had finished connecting, risking a hang.

Solution:

Added a dedicated send_ready event with a 5-second timeout guard around every outbound write triggered from the receive thread.


Problem:

Console input and incoming messages overlapped visually due to multithreading.

Solution:

Implemented thread-safe console printing using locks.


๐Ÿ”ฎ Future Improvements

  • ECDHE key exchange for Perfect Forward Secrecy
  • X.509 certificate authentication
  • Digital signatures
  • Sequence numbers for replay protection
  • Automatic key rotation
  • Mutual authentication
  • TLS-like handshake implementation
  • Multi-client support
  • Group encrypted communication
  • GUI application interface

๐Ÿ›  Technologies Used

Programming Language

  • Python

Networking

  • TCP Sockets
  • Multithreading
  • Custom length-prefixed framing protocol

Cryptography

  • RSA-2048
  • RSA-OAEP
  • AES-256-GCM
  • SHA-256

Libraries

  • cryptography

Security Analysis

  • Wireshark

๐Ÿ“„ License

This project is licensed under the MIT License.

See the LICENSE file for more details.


โš  Disclaimer

This project was developed for educational and research purposes only.

It is not intended to replace TLS or other production-grade secure communication protocols.

For real-world deployments, proper certificate management, authentication, replay protection, and Perfect Forward Secrecy should be implemented.


๐Ÿ“ซ Contact

Youssef Ahmed Abdelfatah

๐ŸŒ Portfolio: https://usfahmed.dev

๐Ÿ’ป GitHub: https://github.com/usfa7med

๐Ÿ’ผ LinkedIn: https://linkedin.com/in/usfahmed

โœ‰๏ธ Email: hello@usfahmed.dev

About

RSA-2048 + AES-256-GCM hybrid encrypted server-to-server communication system

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages