1οΈβ£ Hybrid Cryptography Overview
Asymmetric Encryption (X25519)
Symmetric Encryption (ChaCha20)
Data Transmission
2οΈβ£ Detailed Encryption Flow (Sender)
Important Note: Session Key is used only for one message and then discarded (Forward Secrecy).
3οΈβ£ Detailed Decryption Flow (Receiver)
β οΈ Key Note: In version 2.0, you donβt write any additional code for tag separation. The cryptography library handles everything automatically and securely.
4οΈβ£ Detailed Ciphertext + Tag Structure
# Example in Python:
plaintext = b”HELLO” # 5 bytes
cipher = ChaCha20Poly1305(session_key)
result = cipher.encrypt(nonce, plaintext, None)
print(f”Plaintext length: {len(plaintext)}”) # 5
print(f”Result length: {len(result)}”) # 21
print(f”Ciphertext: {result[:-16].hex()}”) # first 5 bytes
print(f”Tag: {result[-16:].hex()}”) # last 16 bytes
5οΈβ£ Complete Message Flow from Sender to Receiver
# Complete Encryption Flow in Sender.py
# 1. Retrieve receiver’s public key
with open(f”keys/{receiver_id}_public.key”, “r”) as f:
public_key_b64 = f.read().strip()
# 2. Create sealed box for asymmetric encryption
public_key = PublicKey(b64decode(public_key_b64))
sealed_box = SealedBox(public_key)
# 3. Generate random session key
session_key = os.urandom(32) # 256-bit
# 4. Encrypt session key with public key
enc_session_key = sealed_box.encrypt(session_key)
# 5. Generate random nonce
nonce = os.urandom(12) # 96-bit
# 6. Encrypt message with ChaCha20-Poly1305
cipher = ChaCha20Poly1305(session_key)
message_bytes = message[“content”].encode(‘utf-8’)
ciphertext_with_tag = cipher.encrypt(nonce, message_bytes, None)
# β includes ciphertext + tag (16 bytes)
# 7. Construct final message
encrypted_message = {
“message_id”: f”{sender_id}-{correlation_id}-{receiver_id}”,
“receiver_client_id”: receiver_id,
“enc_session_key”: b64encode(enc_session_key).decode(‘utf-8’),
“nonce”: b64encode(nonce).decode(‘utf-8’),
“ciphertext”: b64encode(ciphertext_with_tag).decode(‘utf-8’),
“sent_time”: datetime.now(timezone.utc).isoformat()
}
# 8. Send to server
command = f”publish {exchange_name} {routing_key} {json.dumps(encrypted_message)}\n”
writer.write(command.encode(‘utf-8’))
# Complete Decryption Flow in Receiver.py
# 1. Receive encrypted message
message_data = json.loads(message_str)
# 2. Decrypt session key with private key
enc_session_key = b64decode(message_data[“enc_session_key”])
sealed_box = SealedBox(PRIVATE_KEY)
session_key = sealed_box.decrypt(enc_session_key)
# 3. Extract nonce and ciphertext+tag
nonce = b64decode(message_data[“nonce”])
ciphertext_with_tag = b64decode(message_data[“ciphertext”])
# 4. Decrypt and verify message
cipher = ChaCha20Poly1305(session_key)
try:
plaintext_bytes = cipher.decrypt(nonce, ciphertext_with_tag, None)
# β Inside this function:
# – Tag is separated
# – Tag is verified
# – If valid, decrypts
plaintext = plaintext_bytes.decode(‘utf-8’)
print(f”Message received: {plaintext}”)
except InvalidTag:
print(“β Message tampered or corrupted!”)
Enhanced data and fully compliant with international standards.