Skip to content

Instantly share code, notes, and snippets.

@HimbeersaftLP
Last active September 8, 2020 23:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HimbeersaftLP/235e676bcd487ad92c5adb90f1a28e0c to your computer and use it in GitHub Desktop.
Save HimbeersaftLP/235e676bcd487ad92c5adb90f1a28e0c to your computer and use it in GitHub Desktop.
Morse bruteforce
'''
Brute-force a morse code message where the spaces between letters have been lost,
but the spaces between words still exist
Big thanks to this Stackoverflow post: https://stackoverflow.com/a/8375498/
Instructions:
1. Put your "broken" morse message into the morseString variable below
2. Put a newline seperated dictionary into the same folder as this script and name it dict.txt
e.g. from https://github.com/oprogramador/most-common-words-by-language
3. Run the script
'''
morseString = "-......... ..... .- -....-"
dictFileName = "dict.txt"
import sys
MORSE_TO_ABC = dict(zip([
'.-', '-...', '-.-.', '-..', '.', '..-.', '--.', '....',
'..', '.---', '-.-', '.-..', '--', '-.', '---', '.--.',
'--.-', '.-.', '...', '-', '..-', '...-', '.--', '-..-',
'-.--', '--..'
], 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'))
replaceWords = {
"Ä": "AE",
"Ö": "OE",
"Ü": "UE",
"ß": "SS"
}
WORDS = set()
for word in open(dictFileName).readlines():
word = word.strip().upper()
for a,b in replaceWords.items():
word = word.replace(a, b)
WORDS.add(word)
possibleAbcWordsForMorseWord = {}
def decodeMorseWord(morseWord, thorough=False):
if (morseWord in possibleAbcWordsForMorseWord):
return possibleAbcWordsForMorseWord[morseWord]
possibleLetters = {}
for i in range(0, len(morseWord)):
possibleLetters[i] = {}
# print(f"\nCurrent: {morseSign} {i:02d}", end=" ")
currentSign = ""
for j, additionalMorseSign in enumerate(morseWord[i:]):
currentSign += additionalMorseSign
# print(additionalMorseSign, end="")
if (currentSign in MORSE_TO_ABC):
abcLetter = MORSE_TO_ABC[currentSign]
# print(abcLetter, end="")
possibleLetters[i][abcLetter] = i + j + 1
possibleAbcWordsForMorseWord[morseWord] = []
def findWords(index, word):
if (index == len(morseWord)):
if (not word in possibleAbcWordsForMorseWord[morseWord]):
if (word in WORDS):
possibleAbcWordsForMorseWord[morseWord].append(word)
elif (thorough):
for dictWord in WORDS:
if word in dictWord:
possibleAbcWordsForMorseWord[morseWord].append(word + " (" + dictWord + ")")
else:
for letter, nextIndex in possibleLetters[index].items():
findWords(nextIndex, word + letter)
for letter, nextIndex in possibleLetters[0].items():
findWords(0, "")
return possibleAbcWordsForMorseWord[morseWord]
morseWords = morseString.split(" ")
longestWordLen = len(max(morseWords, key=len))
for morseWord in morseWords:
print(f"Morse: {morseWord}", end="")
print(" " * (longestWordLen - len(morseWord)), end=" ")
sys.stdout.flush()
abcWords = decodeMorseWord(morseWord)
print(abcWords)
print("== DONE ==")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment