Morse Code — it is a method of transmitting textual information in the form of a sequence of tones, lights or clicks that can be understood by an experienced listener or observer without special equipment. It is named after Samuel FB Morse, the inventor of the telegraph.
The algorithm is very simple. Every character in English is replaced by a series of "dots" and "dashes", and sometimes just single "dots" or "dashes" and vice versa.
Please refer to this Wikipedia image for details.
- In the case of encryption, we extract each character (if not a space) from a word one at a time and match it with the corresponding Morse code stored in any the data structure we have chosen (if you are coding in python, dictionaries can be very useful in this case)
- Store the Morse code in a variable that will contain our encoded string, and then we will add to our string a space that will contain the result.
- When encoding in 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 have traversed the entire line
- In case of decryption, we start by adding a space at the end of the line to decode (this will be explained later).
- Now we continue to extract characters from the string until we have a space.
- Once we have a space, we look for the appropriate English character for the extracted character sequence (or our Morse code) and add it to a variable that will hold the result.
- Remember that space tracking is the most important part of this decryption process. As soon as we have 2 consecutive spaces, we add another space to our variable containing the decoded string.
- The last space at the end of the line will help us determine the last sequence of Morse code characters (since the space acts as a check for extracting characters and start decoding).
Python provides a data structure called dictionary, which stores information as key-value pairs, which is very handy for implementing a cipher like Morse code. We can store the Morse code in a dictionary where (key-value pairs) =" (English characters — Morse code) . The plaintext (English characters) replaces the keys, and the ciphertext (Morse code) forms the values of the corresponding keys. Key values can be accessed from a dictionary in the same way we access array values through their index and vice versa.