Classical ciphers, cryptanalysis, and frequency-analysis attacks implemented in Python.
- Introduction
- How to Run Everything
- Key Space Size of the Four Ciphers
- C1 — Break Caesar Performance
- C2 — Break Affine Performance
- C3 — Break Vigenère Performance
- Monoalphabetic Cipher vs AES-128
- Limitations and Improvements
This project implements several classical cryptographic algorithms and basic cryptanalysis techniques.
Implemented ciphers:
| Cipher | Type |
|---|---|
| Caesar cipher | Shift cipher |
| Affine cipher | Linear substitution |
| Monoalphabetic substitution cipher | Full permutation |
| Vigenère cipher | Polyalphabetic |
The project also includes tools for breaking some of these ciphers using frequency analysis and other cryptanalysis techniques.
The program is executed from the command line using:
python3 crypto.py <command> <action> [options]Encrypt
python3 crypto.py caesar encrypt --key 3 --in message.txt --out cipher.txtDecrypt
python3 crypto.py caesar decrypt --key 3 --in cipher.txt --out message.txtBreaking
python3 crypto.py break caesar --in cipher.txt --lang enThe
--langoption specifies the language used for frequency analysis.
Encrypt
python3 crypto.py affine encrypt --a 5 --b 8 --in message.txt --out cipher.txtDecrypt
python3 crypto.py affine decrypt --a 5 --b 8 --in cipher.txt --out message.txtThe value of
amust be coprime with 26.
Breaking
python3 crypto.py break affine --in cipher.txtEncrypt
python3 crypto.py mono encrypt --keyword CRYPTO --in message.txt --out cipher.txtDecrypt
python3 crypto.py mono decrypt --keyword CRYPTO --in cipher.txt --out message.txtThe keyword is used to construct the substitution alphabet.
Encrypt
python3 crypto.py vigenere encrypt --key LEMON --in message.txt --out cipher.txtDecrypt
python3 crypto.py vigenere decrypt --key LEMON --in cipher.txt --out message.txtBreaking
python3 crypto.py break vigenere --m 5 --in cipher.txtThe
--moption specifies the assumed length of the Vigenère key.
The --in option specifies the input file.
The --out option is optional. If it is provided, the result is written to that file:
python3 crypto.py caesar encrypt --key 3 --in message.txt --out cipher.txtIf --out is not provided, the result is printed directly to the terminal:
python3 crypto.py caesar decrypt --key 3 --in cipher.txtCharacters that are not part of the alphabet are ignored during encryption and decryption. Therefore, spaces and punctuation are not preserved in their original positions.
The Caesar Cipher uses a single integer shift over the 26 letters of the alphabet. Therefore the key space is "26". The shift of 0 is equal to non encryption, thus, there are 25 keys in reality.
This cipher uses 2 parameters, (a,b), the value of b can be any of the 26 alphabet positions. But, a must be relatively prime to 26 so that a modinv exists.
The total number of possible keys is:
The monoalphabetic uses a permutation of the 26 letters. Therefore the keyspace is 26!
This cipher depends directly on the key length.
For example if we have a key length of 5 we have
To evaluate the reliability and performance of the Break Caesar frequency based cryptanalysis, two random stories in Spanish and English were generated from which the different length texts came. Each fragment was encrypted using a randomly selected key, and the recovery accuracy (percentage of correctly decrypted keys/texts) was measured across three different scenarios:
- Spanish Text and Spanish Frequency Table
- English Text and English Frequency Table
- Spanish Text and English Frequency Table (using the wrong table)
Where the Frequency Tables are the statistical % of appearance of the letters on different languages, saved in frequencies.py.
| Text Length | Spanish | English | ES Text + ENG Table |
|---|---|---|---|
| 1 | 12.0% | 10.0% | 15.0% |
| 5 | 61.0% | 52.5% | 47.5% |
| 10 | 88.0% | 77.5% | 70.0% |
| 20 | 98.5% | 93.5% | 78.0% |
| 30 | 97.0% | 100.0% | 92.5% |
| 40 | 100.0% | 100.0% | 91.0% |
| 60 | 100.0% | 100.0% | 99.5% |
| 100 | 100.0% | 100.0% | 100.0% |
The breaker becomes reliable, let's fix a 90% accuracy to measure that. Thus, it becomes reliable reaching the 20 letter length text in both cases EN and ES.
The wrong table costs that in that 20 letter length the cipher is not reliable with a 78%, we need 30 letters to reach the 90%. Thus, we lose a 1/3 of the capacity to analyze short texts.
Same setup as Caesar's but dealing with a higher key space, from k = n to k = (a,b).
| Length | ES | EN | ES Text + ENG Table |
|---|---|---|---|
| 1 | 0.5% | 0.5% | 1.0% |
| 5 | 14.0% | 10.5% | 11.5% |
| 7 | 28.5% | 26.0% | 18.0% |
| 10 | 51.5% | 31.5% | 32.5% |
| 20 | 85.5% | 74.0% | 53.5% |
| 30 | 97.5% | 87.5% | 66.0% |
| 40 | 97.5% | 97.5% | 75.0% |
| 60 | 97.0% | 100.0% | 82.5% |
| 100 | 100.0% | 100.0% | 97.5% |
Number of candidates tested
For the affine cipher, the breaker tries every combination of a and b. a must be coprime with 26, thus, there are only 12 valid values for a. This gives us 12 x 26 = 312 candidate keys.
Shortest reliable length and comparison with C1
Using the same 90% reliability threshold defined earlier, the affine breaker reaches it at length 30 for ES texts and 40 for ENG texts. Using the wrong frequency table we can't reach the 90% of reliability until the length of 100.
Comparison with Caesar's — C1
The Caesar's breaker reached the 90% with length 20 in both languages, the affine needs higher texts x1.5 and x2 compared with the caesar's.
The Vigenère is tested over greater lengths, [60, 120, 200, 300], and with these different lengths we can measure how secure different keys are.
The keys are: 3(HOL), 5(ARIHN), 7(ALEXDFZ) with different lengths each one. Here we can see the complexity of this cipher when the key is longer.
Tested over 200 iterations we measure the different accuracies % in the following table.
ES
| Length | HOL (m=3) | ARIHN (m=5) | ALEXDFZ (m=7) |
|---|---|---|---|
| 60 | 91.5% | 58.5% | 15.0% |
| 120 | 100.0% | 97.0% | 61.5% |
| 200 | 100.0% | 100.0% | 85.5% |
| 300 | 100.0% | 100.0% | 93.0% |
EN
| Length | HOL (m=3) | ARIHN (m=5) | ALEXDFZ (m=7) |
|---|---|---|---|
| 60 | 80.0% | 37.0% | 6.5% |
| 120 | 98.5% | 88.0% | 49.5% |
| 200 | 100.0% | 100.0% | 85.0% |
| 300 | 100.0% | 100.0% | 100.0% |
What it really matters is not the total length of the ciphered text, instead the sequence length.
The attack divides the text into different sub sequences one for each of the key, and it applies the caesar's individually.
Thats why with length(60) and HOL(m=3) we have subseq of 20 char and gives us a 91% of precision. Otherwise ALEXDFZ(m=7), gives us only 8 to 9 characters and a 15% of precision.
A bigger key makes Vigenère more secure without changing the algorithm because it cut the text in more subsequences, each one shorter. With a long enough key we can stop the attack.
The key space is not the only thing that matters when we are talking about security.
The monoalphabetic has letter frequencies and has patterns also.
AES-128, despite it has a fewer number of keys (2^128), leaks nothing due to two mechanisms (confusion, diffusion).
Confusion makes the relationship between the key and the text much more complicated and diffusion makes it so that every bit of the original text matters in the cipher.
So brute force is the only option to break the AES-128.
Some limitations of the implementation are:
- Characters outside the project's alphabet are ignored, so spaces and punctuation are not preserved in the output.
- A possible improvement would be to preserve the original text formatting (spaces, periods, and commas) in the output, instead of only returning the raw sequence of alphabet characters as it currently does.
- Frequency-based cryptanalysis is less reliable on short ciphertexts, since longer ciphertexts provide more reliable statistical information. This affects the Caesar and Affine breakers directly.
- The Vigenère breaker assumes the key length (m) is known or correctly guessed in advance; an incorrect m makes the attack unreliable regardless of ciphertext length.
Some LLMs as ChatGPT and Claude where used as a support tool, helping with some tasks:
- Test data generation (ChatGPT): used to create the
en_textandes_textreference corpora used as input for the breaking tests, lives in breack_caesar_test.py. - Documentation formatting (Claude): used to improve the visual presentation of this
README.md, without altering its technical content. - Code assistance (ChatGPT): used to help write parts of
crypto.py, specifically the command-line execution logic.