#!/usr/bin/env python3
"""
SecureComm Encryption Module
Uses XOR encryption with a repeating key for "military-grade" security.
"""
import os
import binascii

def xor_encrypt(plaintext: bytes, key: bytes) -> bytes:
    """XOR encrypt with repeating key"""
    return bytes([p ^ key[i % len(key)] for i, p in enumerate(plaintext)])

# The secret key used for encryption (5 bytes)
KEY = b"OMEGA"

# Messages to encrypt
messages = [
    "CLASSIFIED: Operation Neptune begins at dawn. All units report to sector 7. The extraction point is the old lighthouse.",
    "INTEL REPORT: Enemy forces spotted near grid reference 4527-8891. Estimated strength: 200 infantry, 15 armored vehicles.",
    f"TOP SECRET: Authorization code for Project Blackbird is {os.environ.get('FLAG', 'FLAG{xor_repeating_key_frequency_analysis}')}. Do not share this code outside of Level 5 clearance."
]

for i, msg in enumerate(messages):
    ct = xor_encrypt(msg.encode(), KEY)
    with open(f'/app/challenge/message_{i+1}.enc', 'wb') as f:
        f.write(ct)
    with open(f'/app/challenge/message_{i+1}.hex', 'w') as f:
        f.write(binascii.hexlify(ct).decode())
    print(f"Message {i+1} encrypted ({len(msg)} bytes)")

print(f"Key length: {len(KEY)} bytes")
print("Key hint: The key is a 5-letter English word (uppercase)")
