我一直在尝试创建一个凯撒密码程序与信息'我的秘密'和键6-所有的字母在密码字母表中向右移动6个位置,当字母从末尾掉下来时,字母“环绕”。使用密码,消息“我的秘密”将被编码为“gsumzxlzn”。但是,我保留了错误的编码和解码结果。结果是:
编码:sdfykixkz
解码:abcdefghi
请帮忙!在import sys
ALPHABET= ['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',' ']
def main(plaintext, key):
cypher = generate_cypher(key)
plaintext = sys.argv[1]
e_code = encode_message(plaintext, cypher)
mes s= decode_message(e_code, cypher)
def generate_cypher(key):
cipher = []
for i in range(len(ALPHABET)):
cipher.append(ALPHABET[i+key%27])
print(cipher)
return(cipher)
def encode_message(plaintext,cipher):
key = int(sys.argv[2])
en_code=''
for c in plaintext:
if c in cipher:
en_code+=cipher[(cipher.index(c)+key)%(len(cipher))]
print("Encoded: ", en_code)
return en_code
def decode_message(e_code,cipher):
key = int(sys.argv[2])
message = []
for i in range(len(e_code)):
message.append(cipher[i-key%27])
mess=''.join(message)
print("Decoded: ", mess)
return mess