Morse code is a method of transmitting text information as a series of on-off tones, lights or clicks. It can be directly translated by a familiar partner without special equipment. It is named after Samuel F. B. Morse, the inventor of the telegraph.
algorithm
The algorithm is very simple. Every character in English is replaced by a series of "dots" and "dashes", or sometimes just singular "dots" or "dashes", and vice versa.
encryption
- In the case of encryption, we extract each character (if not a space) from the word one at a time and match it with the corresponding Morse password stored in any data structure we choose (if you use python coding, the dictionary can become very useful in this case)
- Store the Morse password in a variable that will contain the string we encode, and then we add a space to the string containing the result.
- When encoding with Morse code, we need to add 1 space between each character and 2 consecutive spaces between each word.
- If the character is a space, add another space to the variable containing the result. We repeat this process until we traverse the entire string
decrypt
- In the case of decryption, we first add a space at the end of the string to be decoded (which will be explained later).
- Now we continue to extract characters from the string until we don't have any space.
- Once we get a space, we will find the corresponding English character in the extracted character sequence (or our Morse code) and add it to the variable that will store the result.
- Remember that trace space is the most important part of this decryption process. Once we get two consecutive spaces, we add another space to the variable containing the decoded string.
- The last space at the end of the string will help us identify the last sequence of Morse code characters (because the space acts as a check to extract characters and begin decoding them).
implement
Python provides a data structure called dictionary, which stores information in the form of key value pairs, which is very convenient for implementing passwords such as Morse code. We can save the Morse code table in the dictionary, where (key value pair) = > (English character Morse code). Plaintext (English characters) replaces the key, and ciphertext (Morse password) forms the value of the corresponding key. The value of the key can be accessed from the dictionary, just as we access the value of the array through the index, and vice versa.
Morse code comparison table
# Python program to implement Morse cipher translator ''' VARIABLE KEY 'cipher' -> 'Morse translation form for storing English strings' 'decipher' -> 'Stores the English translation form of Morse string' 'citext' -> 'Morse password for storing a single character' 'i' -> 'Calculates the space between Morse characters' 'message' -> 'Stores the string to encode or decode ''' # A dictionary representing Morse cipher diagrams MORSE_CODE_DICT = { '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':'--..', '1':'.----', '2':'..---', '3':'...--', '4':'....-', '5':'.....', '6':'-....', '7':'--...', '8':'---..', '9':'----.', '0':'-----', ', ':'--..--', '.':'.-.-.-', '?':'..--..', '/':'-..-.', '-':'-....-', '(':'-.--.', ')':'-.--.-'} # A function that encrypts a string according to the moss cipher graph def encrypt(message): cipher = '' for letter in message: if letter != ' ': # Look up the dictionary and add the corresponding Morse password # Morse passwords with spaces separating different characters cipher += MORSE_CODE_DICT[letter] + ' ' else: # 1 space indicates different characters # 2 means different words cipher += ' ' return cipher # A function that decrypts a string from moss to English def decrypt(message): # Add extra space at the end to access the last Morse password message += ' ' decipher = '' citext = '' for letter in message: # Check space if (letter != ' '): # Counter to track space i = 0 # In the case of spaces citext += letter # In the case of space else: # If i = 1, it indicates a new character i += 1 # If i = 2, it means a new word if i == 2 : # Add spaces to separate words decipher += ' ' else: # Use their values to access the key (encrypted reverse) decipher += list(MORSE_CODE_DICT.keys())[list(MORSE_CODE_DICT .values()).index(citext)] citext = '' return decipher # Hard coded driver functions to run the program def main(): message = "JUEJIN-HAIYONG" result = encrypt(message.upper()) print (result) message = ".--- ..- . .--- .. -. -....- .... .- .. -.-- --- -. --." result = decrypt(message) print (result) message = "I LOVE YOU" result = encrypt(message.upper()) print (result) message = ".. .-.. --- ...- . -.-- --- ..-" result = decrypt(message) print (result) # Execute main function if __name__ == '__main__': main() Copy code
Output:
.--- ..- . .--- .. -. -....- .... .- .. -.-- --- -. --. JUEJIN-HAIYONG .. .-.. --- ...- . -.-- --- ..- I LOVE YOU Copy code
Quick summary -- Python program implementation of Morse cipher translator
The above is the whole content of this article. You use Python program to implement Morse password translator. We hope this blog can help you. The blogger is also learning. If there are any mistakes, we hope to criticize and correct them. If you like this article and are interested in seeing more of it, you can check it out here( Github/Gitee )Here is a summary of all my original works and source code. Pay attention to me to see more articles.