The monoalphabetic cipher is a simple substitution cipher, where each character in a text gets replaced with a new one. Mathematically it is a permutation. Consider the alphabet as a set of 26 characters A to Z. The map that encrypts the text is given by a key with 26 characters, where the first character is the image of A, the second the image of B and so on.
This map preserves statistical properties of languages, which can be used to crack these ciphers. This script aims to do that.
Allthough it works on the given example messages, the results depend heavily on the given parameters and the properties of the cipher text.
usage: monoalpha.py [-h] [-d] [-k KEY] [-p [PARAMETER ...]] {crack,ana,dec,enc,keygen} filename
Try to crack the monoalphabetic cipher.
positional arguments:
{crack,ana,dec,enc,keygen}
The mode of of the script
filename input textfile
options:
-h, --help show this help message and exit
-d, --dev Activates dev mode. The code assumes that the text was encrypted using the trivial key.
-k KEY, --key KEY key for enc-/decryption.
-p [PARAMETER ...], --parameter [PARAMETER ...]
This mode can be used to generate a valid key and print it.
$ ./monoalpha.py keygen message.txt
OCJXWGVZMTKFSUPERADIBQYNHLNote that, allthough it is not used, a filename needs to be given.
Given a valid key encrypt or decrypt the content of a file.
$ ./monoalpha.py -k OCJXWGVZMTKFSUPERADIBQYNHL enc message.txt
MUFMI WAOAH IZWPA HOIWN IMDOU HPCTW JIIZO IJOUC WAWOX YZWIZ WAIZM DPCTW JIMDO YPAKP GFMIW AOIBA WODIA WWIDM VUOUO AAOUV WSWUI PGCBM FXMUV DPUOJ MIHCF PJKPA DIHFW DPGJF PIZMU VMIMD OJPZW AWUID WIPGD MVUDI ZOIIA OUDSM IDDPS WKMUX PGMUG PASOI MQWSW DDOVW [...]Note that, for encryption any characters that differ from the alphabet (A-Z) are ignored. The cipher text is printed in five character long blocks.
$ ./monoalpha.py -k OCJXWGVZMTKFSUPERADIBQYNHL dec cipher.txt
INLITERARYTHEORYATEXTISANYOBJECTTHATCANBEREADWHETHERTHISOBJECTISAWORKOFLITERATUREASTREETSIGNANARRANGEMENTOFBUILDINGSONACITYBLOCKORSTYLESOFCLOTHINGITISACOHERENTSETOFSIGNSTHATTRANSMITSSOMEKINDOFINFORMATIVEMESSAGETHISSETOFSIGNSISCONSIDEREDINTERMSOFTHEINF [...]Decrytption removes the spaces of the cipher text and prints the decrypted text as one long string.
The main tool to crack the monoalphabetic cipher is to analyze the frequency of letters, bigrams and trigrams in the ciphertext. To analyze a cipher text use.
$ ./monoalpha.py ana cipher.txt
LETTERS
i | 153
w | 144
m | 104
a | 88
u | 87
p | 84
o | 81
[...]
BIGRAMS
iw | 36
iz | 35
mu | 27
wa | 25
im | 25
mi | 24
zw | 24
oi | 24
[...]
TRIGRAMS
izw | 19
wui | 14
iwa | 11
iwn | 11
wni | 11
oim | 11
[...]To crack the monoalphabetic cipher the script analyzes the frequencies of letters, bigrams, trigrams and quadgrams. Then it maps the most common n-grams in the english language to the most common n-grams found in the cipher text. This is done via a tree search. After that it tries to match words into the partially guessed text. The language data can be found in the data folder.
$ ./monoalpha.py crack cipher.txt
[...]
KEY ngft red mpft c csts wdft wfreq len
OCJXWG-ZMTKFEUP--ADIB-YNH- 12.27 1386.29 0.00 None 6.67 758.27 21
OCJXWG-ZMTKFEUP--ADI--YNH- 12.27 363.75 0.00 None 7.24 876.22 20
OCJXWGVZMTKFSUP--ADIB-YNH- 12.27 441.82 0.00 None 7.76 957.40 22
OCJXWG-ZMTKF-UP--ADIB-YNH- 12.27 363.90 0.00 None 8.60 480.47 20
OCJXWG-ZMTKF-UP--ADI--YNH- 12.27 191.37 0.00 None 9.45 536.90 19
OCJXWG-ZMT-FEUP--ADI--YNH- 12.27 127.58 0.00 None 7.35 1036.26 19
OCJXW--ZMT-F-UP--ADI--YNH- 12.27 71.18 0.00 None 7.68 470.06 17
min: OCJXW--ZMT-F-UP--ADI--YNH-
=== DECRYPT WITH GUESSES ===
decrypting with minimum guess
INLITERARYTHEORYATEXTISANYOBJECTTHATCANBEREADWHETHERTHISOBJECTISAWORkOgLITERATbREASTREETSIvNANARRANvEsENTOgBbILDINvSONACITYBLOCkORSTYLESOgCLOTHINvITISACOHERENTSETOgSIvNSTHATTRANSsITSSOsEkINDOgINgORsATIqEsESSAvETHISSETOgSIvNSISCONSIDEREDINTERsSOgTHEINgORsATIqEsESSAvESCONTENTRATHERTHANINTERsSOgITSeHYSICALgORsORTHEsEDIbsINWHICHITISREeRESENTEDWITHINTHEgIELDOgLITERARYCRITICISsTCompare the min key with the key generated above!
NOTE THAT THE SUCCES OF THE CRACK HIGHLY DEPENDS ON THE PARAMETER AND ON THE GIVEN CIPHER TEXT
- SEARCH_N: A flag which enables the tree search for N-grams
- DEPTH_N: Defines the number of recursions into the tree. For N=W this referes to the matching of words.
- FILTER_BRANCH_N: A percentage that only includes that portion of the current level for the next recursion.
parameters = {
"SEARCH_3": True,
"DEPTH_3": 5,
"FILTER_BRANCH_3": 1,
"SEARCH_2": True,
"DEPTH_2": 9,
"FILTER_BRANCH_2": 0.7,
"SEARCH_1": False,
"DEPTH_1": 8,
"FILTER_BRANCH_1": 0.5,
"DEPTH_W": 17}