1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import itertools # creates iterators for efficient looping
# dictionary of mapping as on
# https://en.wikipedia.org/wiki/Morse_code#Letters,_numbers,_punctuation,_prosigns_for_Morse_code_and_non-English_variants
morse = {
"a": ".-",
"b": "-...",
"c": "-.-.",
"d": "-..",
"e": ".",
"f": "..-.",
"g": "--.",
"h": "....",
"i": "..",
"j": ".---",
"k": "-.-",
"l": ".-..",
"m": "--",
"n": "-.",
"o": "---",
"p": ".--.",
"q": "--.-",
"r": ".-.",
"s": "...",
"t": "-",
"u": "..-",
"v": "...-",
"w": ".--",
"x": "-..-",
"y": "-.--",
"z": "--..",
"0": "-----",
"1": ".----",
"2": "..---",
"3": "...--",
"4": "....-",
"5": ".....",
"6": "-....",
"7": "--...",
"8": "---..",
"9": "----.",
".": ".-.-.-",
",": "--..--",
"?": "..--..",
"'": ".----.",
"!": "-.-.--",
"/": "-..-.",
"(": "-.--.",
")": "-.--.-",
"&": ".-...",
":": "---...",
";": "-.-.-.",
"=": "-...-",
"+": ".-.-.",
"-": "-....-",
"_": "..--.-",
"\"": ".-..-.",
"$": "...-..-",
"@": ".--.-.",
" ": ""
}
revMorse = {} # Dictionary containing inverse mapping from morse code to its corresponding encoded character
for key, value in morse.items():
revMorse[value] = key
def decrypt_morse(message):
"""
Finds the decryption of `message` encoded in morse with individual
letters separated by x and words separated by xx
"""
words = message.split('x')
return "".join(revMorse[word] for word in words)
# Checking the example given in the pdf
print(decrypt_morse("---x-.x-.-.x.xx..-x.--.x---x-.xx.-xx-x..x--x.x"))
ct = "118289293938434193849271464117429364476994241473157664969879696938145689474393647294392739247721652822414624317164228466"
substitution_token = ['..', '.-', '.x', '-.', '--', '-x', 'x.', 'x-', 'xx']
for perm in itertools.permutations(range(9)):
# Iterate over permutations of substitution_tokens to find the
# permutations which finds the valid decryption
ct1 = ct
for i in range(9):
ct1 = ct1.replace(str(i + 1), substitution_token[perm[i]])
# Replace digit in ciphertext with given permutation of substitutions
try:
if 'flag' in decrypt_morse(ct1):
# if the decyrpted strings containts flag, we get our string
print(decrypt_morse(ct1))
break
except KeyError:
continue
# Output
# once upon a time
# congratulations. please wrap this message in a flag format: m0r3_b1t5