Created
February 22, 2018 21:40
-
-
Save iandioch/270cb29e4db07db9614acdcc0b8fe41a to your computer and use it in GitHub Desktop.
Crack a monoalphabetic cypher
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ORDERED_ENGLISH = [c for c in "ETAOINSHRDLCUMWFGYPBVKJXQZ"] | |
# Dict mapping cypher letter to decrypted letter. Was empty in the beginning. | |
# Built up piece by piece, first by finding the H between T's and E's to create the | |
# words "the", "then", "them", "their". Confirmed "PYH" -> "THE". | |
# Noticed garbled form of the word "sincerity" in the middle of the text. Added | |
# mappings to fix it. Confirmed "AO" -> "RI". | |
# Text started to take shape. Noticed lots of garbled "of"s across the text. | |
# Added "L" -> "O". Also added "Z" -> "W" to fix "whatever". | |
known = { | |
'H': 'E', | |
'Y': 'H', | |
'A': 'R', | |
'O': 'I', | |
'L': 'O', | |
'Z': 'W', | |
'R': 'F', | |
'B': 'Y', | |
'E': 'P', | |
'D': 'B', | |
'S': 'J', | |
'X': 'U', | |
'T': 'C', | |
'C': 'L', | |
'K': 'Q', | |
} | |
with open('text.in', 'r') as f: | |
CYPHER_TEXT = f.read().strip() | |
ORDERED_CYPHER = sorted(ORDERED_ENGLISH, key=lambda x: CYPHER_TEXT.count(x), reverse=True) | |
print(CYPHER_TEXT) | |
d = {} | |
values = set() | |
for k in known: | |
d[k] = known[k] | |
values.add(known[k]) | |
i = 0 | |
j = 0 | |
while i < 26 and j < 26: | |
english = ORDERED_ENGLISH[i] | |
cypher = ORDERED_CYPHER[j] | |
while english in values: | |
i += 1 | |
english = ORDERED_ENGLISH[i] | |
while cypher in d: | |
j += 1 | |
cypher = ORDERED_CYPHER[j] | |
d[cypher] = english | |
i += 1 | |
j += 1 | |
print(d) | |
print(''.join(d[c] for c in CYPHER_TEXT)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
YHAHKXHIPHJPLMGLZORVUHHPOGNLRPYHEHLECHDBPYHOAJHEXPOHIZLXCJDHEHAUOPPHJDBPYHVAUBVPVGBNOFHGELOGPLGPYHOAUVATYOGPLPYVPTLXGPABZOPYRAHIYHFOJHGTHLRPYHIOGTHAOPBLRPYHOAJOIELIOPOLGPLVTKXOHITHOGZYVPHFHAUONYPDHAHKXOAHJOAHECOHJOIVZGLLDSHTPOLGPLOPEALFOJHJPYHBTVUHXGVAUHJDXPPLDHTVXPOLXIPYVPGLPVNXGZVIROAHJVIPYHAHTLXCJDHGLVGIZHAOGNRLATLGIHKXHGTHIOGPYOITVIHOVIIXAHJPYHUPYVPHFHABELIIODCHTVAHIYLXCJDHPVMHGPLMHHEPYHPALLEIRALULRRHAOGNPYHUVGBOGIXCPLAJVUVNHVGJPYVPPYLIHZYLVCZVBIYVJDHHGIXDLAJOGVPHPLPYHCVZIIXTYVIYVJVFVOCHJPYHUIHCFHILRPYHVUGHIPBIYLXCJGLPDHOGSXAHJOGPYHOAEHAILGILAEALEHAPBVGJPYVPPYHPAHVPUHGPLRPYHAHIPZLXCJJHEHGJXELGPYHOALZGTLGJXTPPYVPPYHVAUBXGCHIILEELIHJJOJGLPUHVGPLVTPVIHWHTXPOLGHAILADAOGNLRRHGJHAIPLVUOCOPVABPAODXGVCDXPUHAHCBPLVOJPYHTOFOCUVNOIPAVPHIZOPYZYLULRRHGTHIZLXCJCBHPYXIHGJIPYHUVPPHA |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HEREQUESTEDTOKNOWIFAMEETINGOFTHEPEOPLEBYTHEIRDEPUTIESWOULDBEPERMITTEDBYTHEARMYATANYGIVENPOINTONTHEIRMARCHINTOTHATCOUNTRYWITHFRESHEVIDENCEOFTHESINCERITYOFTHEIRDISPOSITIONTOACQUIESCEINWHATEVERMIGHTBEREQUIREDIREPLIEDISAWNOOBJECTIONTOITPROVIDEDTHEYCAMEUNARMEDBUTTOBECAUTIOUSTHATNOTAGUNWASFIREDASTHERECOULDBENOANSWERINGFORCONSEQUENCESINTHISCASEIASSUREDTHEMTHATEVERYPOSSIBLECARESHOULDBETAKENTOKEEPTHETROOPSFROMOFFERINGTHEMANYINSULTORDAMAGEANDTHATTHOSEWHOALWAYSHADBEENSUBORDINATETOTHELAWSSUCHASHADAVAILEDTHEMSELVESOFTHEAMNESTYSHOULDNOTBEINJUREDINTHEIRPERSONSORPROPERTYANDTHATTHETREATMENTOFTHERESTWOULDDEPENDUPONTHEIROWNCONDUCTTHATTHEARMYUNLESSOPPOSEDDIDNOTMEANTOACTASEXECUTIONERSORBRINGOFFENDERSTOAMILITARYTRIBUNALBUTMERELYTOAIDTHECIVILMAGISTRATESWITHWHOMOFFENCESWOULDLYETHUSENDSTHEMATTER | |
- quote from President Washington's Diaries 1791-1799 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment