Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to crack Morse Code

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to use Python to crack the Morse code. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Morse code

The Morse code is defined as follows:

Morse code (also translated as Morse code, English: Morse code) is a kind of time-on and off signal code, which expresses different English letters, numbers and punctuation marks in different order. It was invented by American Alfidwell and Samuel Morse in 1836.

Morse code is an early form of digital communication, which relies on a series of dots and strokes to transmit coded information.

Point (): 1 (read "Di" dit, time occupies 1t)

Draw (-): 111 (read "da" dah, time occupies 3t)

Pause within the character (between the dot and the dash): 0 (time occupies 1t)

Intercharacter pause: 000 (time occupies 3t)

Pause between words: 0000000 (time occupies 7t)

The length of the point (that is, the time length t above) determines the speed of transmission.

The comparison of our letters, numbers and punctuation with Morse code is as follows:

We are now going to send the word "M O R S E (space) C O D E" (morse code). As you can see by looking up the table, it should be like this.

The corresponding message should be as follows (drop for knock, ▢ for pause)

Didi ▢ Didi ▢▢▢ Didi ▢ Didi ▢ Didi ▢▢▢ Didi ▢▢▢ Didi ▢ Didi ▢▢▢ Didi

Isn't that interesting?

Python implementation

Using Python to realize the encryption and decryption of Morse password is actually very simple. You only need to put the comparison table in a dictionary, split the plaintext when encrypting, and then take the corresponding passwords from the dictionary and combine them together. During decryption, you can find the corresponding plaintext through the ciphertext comparison table and put it together.

Morse code comparison table

After we store the Morse password comparison table in a dictionary, it goes like this:

MORSE_CODE_DICT = {'Agar:'. -', 'Mr.:' -', 'Mr.:' -.', 'Mr.:'., 'Mr. 'Illustrated:'..', 'Jake:'. -', 'Knights:' -., 'Lindsay:'. -.., 'Mises:'--', 'Nasty:' -., 'Olympian:'--', 'playing:'.--., 'Qothers:'--. 'Renewal:'. -', 'Slaughter:'..., 'Turing:' -', 'Utility:'. -', 'Vince:'... -', 'Waring:'.--', 'Xerox:' -. -', 'Yee:'.--', 'Zhuan:'--.. '1percent:'.--', '2percent:'.--', '3percent:'., '4percent:', '5percent:'., '6percent:' -., '7percent:' -., '8percent:'--. '913:'--.','0':'-,',':'-.--','.:'. -',':'. -',':'. '(':'. -.','): encryption

The process of encryption is the process of translating plaintext into ciphertext through a comparison table.

We read the plaintext one by one. If it is letters, numbers or punctuation marks, go to the dictionary to find the corresponding password. The characters are separated by spaces. If it is the space between words, add two consecutive spaces to separate the words.

The code for the encryption process is as follows:

Def encrypt (message): cipher =''for letter in message: if letter! ='': # look up the dictionary and add the corresponding Morse code # separate different characters with spaces cipher + = MORSE_CODE_ DICT [letter] +''else: # 1 space represents different characters # 2 denotes different words cipher + =''return cipher decryption

In the case of decryption, we first add a space at the end of the string to be decoded, and we extract characters from the string.

Once we get a space, we look for the corresponding English character in the extracted character sequence (or our Morse code) and add it to the variable where the result will be stored.

Once we get two consecutive spaces, we add another space to the variable that contains the decoded string.

The last space at the end of the string will help us identify the last sequence of Morse code characters.

The code for the decryption process is as follows:

# the function def decrypt (message) that decrypts the string from Morse to English: # add extra space at the end to access the last Morse password message + =''decipher =' 'citext =' 'global i for letter in message: # check the space if letter! =': I = 0 # in the space Citext + = letter # in the case of space else: # if I = 1 represents a new character I + = 1 # if I = 2 represents a new word if I = = 2: # add spaces to separate the word decipher + =' 'else: # use their value to access the key (reverse encryption) decipher + = list (MORSE_CODE_DICT.keys ()) [list (MORSE_CODE_DICT.values ()) .index (citext)] citext =' 'return decipher test

Let's first test the encryption algorithm:

Message = "I LOVE YOU" result = encrypt (message.upper ()) print (result)

The result of the operation is:

.. . -. -.-

You can check the mapping table to see if it is correct.

Test the decryption algorithm again:

Message = ".. -.. -. -" result = decrypt (message) print (result)

The result of the operation is:

I LOVE YOU

The complete code #! / usr/bin/env python3#-*-coding: utf-8-*-"@ author: Xian Huan" # represents the dictionary of Morse cipher graph MORSE_CODE_DICT = {'Aguilar:'. -', 'dating:' -...', 'dating:' -.', 'dating:' -.', 'Egg:'.' 'Fitch:'. -.', 'Globe:'--., 'Hallows:'....,'I'm sorry:'.., 'Jake:'., 'Knights:'--', 'Little:'. -.., 'Malley:'--', 'Nice:' -'. 'Oval:'- -', 'purveyor:'.--., 'Qothers:'--., 'ringing:'. -., 'Shear:'., 'Turing:' -', 'Utility:'. -', 'Venture:'. -', 'Wells:'. 'Xray:'-. -', 'Yee:'., 'Zhuan:'--., '1century:'. -', '2percent:'..--', '3percent:'.. -', '4percent:'. '613:' -.','7':'-.,'8':'-.,'9:','-.,'0:','-,':. '?':'.--.','/':'. -','-':'-','(':'-. Function def encrypt (message): cipher = 'for letter in message: if letter! ='': # look up the dictionary and add the corresponding Morse code # Morse code with different characters separated by spaces Cipher + = MORSE_CODE_ DICT [letter] +''else: # 1 space for different characters # 2 for different words cipher + =' 'return cipher# decrypts the string from Morse to English function def decrypt (message): # add extra space at the end to access the last Morse code Message + =''decipher =' 'citext =' 'global i for letter in message: # check the space if letter! ='': I = 0 # in the case of spaces citext + = letter # in the case of space else: # if I = 1 indicates a new The character I + = 1 # if I = 2 represents a new word if I = = 2: # add spaces to separate the word decipher + =''else: # use their value access key (the reverse of encryption) decipher + = list (MORSE_CODE_DICT.keys ()) [list (MORSE_CODE_DICT.values ()) .index (citext)] citext =''return decipherdef main (): message = "I LOVE YOU" result = encrypt (message.upper ()) print (result) message = ". . -. -.-"result = decrypt (message) print (result) # execute the main function if _ _ name__ = ='_ main__': main () this is the end of the article on" how to use Python to crack Morse Code ". I hope the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good. Please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report