如何使用基于伪随机数的字母对给定字符串进行编码,使用列表、字符串方法、字典?

How to use letters based on pseudo-random numbers to encode a given string, using lists, string methods, dictionaries?

字母表 table:

Letter - `a, b, c, d, ... z`.  
Number - `0, 1, 2, 3, ... 25`.

编码Table:

Number - `0, 1, 2, 3, ...25`.  
Code (from random seed 202090) - `23, 15, 1, 22, ...` (same length as letters above).

利用Table1和Table2,任意字母都可以编码如下:

  1. 在Table1中查找字母的编号(例如'm'对应编号12)。

  2. 查找Table2中的代码(例如数字12对应代码10)

  3. 往回查找Table1中的编码得到编码字母(例如编码10对应 字母 'k').

  4. 将编码字母转换为大写(例如编码字母'k'转换为'K')。

这是实际编码的第二部分 message/file 我正在努力处理的内容。

不允许导入标准随机模块以外的模块。

必须使用字符串方法、列表和字典。

import random

def main():
    user = int(input('Enter a number: '))
    random.seed(user)
    message = input('Enter message: ') 
    create_code_list(user)
    encode(user,message)

def create_code_list(user):
    letter = 'abcdefghijklmnopqrstuvwxyz'
    code_list = (random.sample(range(0,26),len(letter)))
    print (code_list)
    return code_list

def encode(code_list,message):
    letter = 'abcdefghijklmnopqrstuvwxyz'
    letter_list = list(letter)
    code_list = create_code_list(letter_list)
    string_list = list(string)
    c = {}
    for i in range(len(letter_list)):
        c[string_list[i]] = letter[i]
    
main()

好的,你想对给定的单词进行编码和解码吗?请参阅我在 git-hub: Simple-Cryptography 中发布的 repo。代码可能很长。请参阅此代码:

#An inbuilt module which contains all the printables
import string

letters = [] #A list where all the printable letters will be added.
numbers = [] #A list which contains the equal number of elements with letters


for letter in string.printable:
    letters.append(letter)  #Append the letter to the list named letters

for number in range(0, len(letters)):
    numbers.append(number)  #Append the number in the list named numbers


#Create a dict that contains all the keys as letters and the values as numbers.
encode_dict = dict(zip(letters, numbers))

#Create a dict that contains all the keys as numbers and the values as letters.
decode_dict = dict(zip(numbers, letters))

#A function that encodes the given key:
def encode():

    #Get the input from user:
    text_to_encode = str(input('The Text To Encode: '))

    #A string which will contain all the decoded numbers:
    encoded_text = ''

    #Append the values for the keys as the letter in the inputted text:
    for letter in text_to_encode:
        encoded_text = encoded_text + str(encode_dict[letter]) + " "   #Include The " "(white space) to make it decodable! !important

    #Print the encoded text
    print(encoded_text)

def decode():

    #Get the input from user:
    text_to_decode = str(input('The Text To Decode: '))

    #a string that contains all the decoded letter:
    decoded_text = ''

    #Append the values for the keys as the letter in the inputted text:
    for letter in text_to_decode.split(): #Remove the white_space to identify the letter .split() !important
        
        decoded_text = decoded_text + str(decode_dict[int(letter)]) #Convert the letter to 'int' because the keys are int !important

    print(decoded_text)

#The end of the code!

当我运行这个:

>>> encode()
The Text To Encode: Whosebug
28 29 10 12 20 24 31 14 27 15 21 24 32 
>>> decode()
The Text To Decode: 28 29 10 12 20 24 31 14 27 15 21 24 32 
Whosebug
>>> 

在此您可以输入键盘上的任何字母或数字!