Think of a fun thing to do, using Python to implement the German device that was encrypted during World War II-Engma.
So what is Engma, and how does he work? This article provides a very detailed description:
https://www.zhihu.com/question/28397034
Please look at the high-ticket answer.
Road step by step, rice stutter, below, we also come step by step to achieve Engma:
Chapter 1
Use Python to implement simple letter substitution:
When I enter a string of strings (the string is not punctuation, only letters and spaces), the program will follow the pre-set letter substitution table, simply replace the password.
Such as:
Abcdefghjiklmnopqrstuvwxyz
Qwertyuiopasdfghjklzxcvbnm
So when I enter Hello World, it should automatically replace me with ITSSG Vgkar
Try it!
The code is as follows: Using the Maketrans method of string, do you have any idea?
#-*-Coding:utf-8-*-# The purpose of this code is to implement the German World War II cipher machine via Python: Engma # First, the first step is to define a simple function: simple character substitution import Reimport stringdef simple_ Replace (password, replace_word): If not type (password) = = Type (replace_word) = = Type (' a '): print (' Password must be a string! ') return False an = Re.match (' [a-z\s]+$ ', password) if not a: print (' string can only contain lowercase letters and spaces!) ') return False If Len (replace_word)! =: print (' The replacement code must be 26 letters!) ') return False ori_table = ' abcdefghijklmnopqrstuvwxyz ' table = Str.maketrans (ori_table, Replace_word ) return str.translate (password, table) A_password = input (' Please enter clear text: ') R_password = ' qwertyuiopasdfghjklzxcvbnm ' Print (' Ciphertext as follows: ', Simple_replace (A_password, R_password))
Python programming challenges-using Python to implement Engma--chapter 1