Designing a Layered Encryption Library: The TTMC Cipher π
"What happens if we combine multiple classical encryption techniques into a single, modular layered system?"
From_Curiosity_to_Library
Cryptography has always fascinated me because of how simple transformations can completely change the readability of information. A plain message can be turned into something unintelligible just by applying a few mathematical rules.
While studying classical cryptographic techniques, I came across two well-known methods: Caesar Cipher and Transposition Cipher. Individually, both are relatively easy to break. But I wondered: What happens if we combine multiple techniques into a layered system?
That question led to TTMC Cipher β a Python library that combines Transposition, a Modified Caesar substitution, and a classical Caesar shift.
Project_Architecture
Instead of a single script, I structured it as a modular Python package where each module handles a specific responsibility.
Generating_Secure_Keys
Before encryption begins, the cipher derives keys from a password and salt. Salt generation is implemented in utils.py to ensure strong randomness.
def generate_salt(length: int = 16) -> str:
# Uses secrets for cryptographically strong randomness
return ''.join(choice(string.ascii_letters + string.digits) for _ in range(length))The key generation module then derives keys using SHA-256 hashing.
The_Encryption_Pipeline
The encryption process is implemented in the TTMCCipher.encrypt() method and follows three distinct stages. Each stage transforms the message differently, introducing both confusion and diffusion.
Stage_01: Transposition
The first stage rearranges characters using a dictionary-based columnar transposition technique.
# Transposition logic snippet stage1, result_dict = transposition_encrypt(key1, plaintext.upper()) # Random numeric keys are generated and mapped to columns keys = random.sample(range(10), len(key1))
The dictionary structure created during this step looks roughly like this:
Sorting these keys changes the order of characters, spreading the influence of individual characters (diffusion). The dictionary is stored for reverse lookup during decryption.
Stage_02: Modified_Caesar
Next, the cipher performs a substitution using a randomized alphabet key, introducing substitution-based confusion.
alphabet_dict = dict(zip(string.ascii_uppercase, key2)) stage2 = "".join(alphabet_dict.get(c, c) for c in stage1)
Stage_03: Classic_Caesar
Finally, a traditional Caesar shift is applied. The shift value is randomly generated within a range of 1 to 25 to ensure a meaningful transformation.
# final_ciphertext = caesar_shift_encrypt(self.shift, stage2) self.shift = random.randint(1, 25)
The_Decryption_Pipeline
Decryption simply reverses the encryption pipeline, working backward from the scrambled ciphertext to reconstruct the original message.
Step_01: Reverse_Caesar
The process begins by reversing the final stage of encryption. The same shift value used during encryption is subtracted from each character.
Step_02: Reverse_Substitution
The modified Caesar stage is reversed by searching the substitution dictionary for matching values and retrieving the original alphabet characters.
Step_03: Reverse_Transposition
Finally, the plaintext is reconstructed using the stored transposition structure. The dictionary values store both encrypted and original characters, allowing for full reconstruction.
Why_Layer_Multiple_Techniques?
Individually, classical ciphers are weak. However, combining multiple transformations introduces two important cryptographic concepts:
Confusion
Substitution obscures the relationship between plaintext and ciphertext, making it harder to identify patterns in the alphabet usage.
Diffusion
Transposition spreads the influence of individual characters across the entire message, disrupting word structures and character frequency.
The TTMC cipher layers these effects by combining permutation (transposition), substitution (modified Caesar), and additional shifting (classic Caesar). This layered structure makes the output significantly more complex than any single classical cipher.
What_I_Learned_From_This_Project
Building this cipher reinforced several important software engineering ideas:
- Modular System DesignSeparating encryption stages into distinct modules made the system significantly easier to extend, test, and debug.
- Algorithmic ThinkingEven simple cryptographic algorithms require careful reasoning about reversibility, modular arithmetic, and edge cases.
- Security FundamentalsUsing techniques like salt generation and hash-based key derivation introduces concepts that are foundational to modern cryptographic systems.
Limitations
Itβs important to emphasize that classical ciphers like Caesar and transposition are not secure for real-world applications. Modern cryptography uses algorithms such as AES and RSA that rely on significantly more complex mathematical structures.
The TTMC cipher should be viewed as:
- A learning tool for exploring cryptographic history.
- A demonstration of layered encryption concepts.
- An exploration of modular cryptographic algorithm design.
Final_Thoughts
The TTMC Cipher project started as a simple exploration of classical cryptographic techniques. By combining transposition, substitution, and Caesar shifts into a modular Python implementation, the project became an interesting exercise in both algorithm design and software architecture.
Sometimes the best way to understand complex systems is to start with simpler ideas and experiment with how they interact. This project was one such experiment β and a great opportunity to explore how layered transformations can produce surprisingly intricate results.