为什么凯撒密码(Python)中有%26?

问题描述 投票:0回答:1

我试图在不知道凯撒密码密钥的情况下,从用户那里解密文本。

我可能大部分都理解,但我不明白为什么他们在第8行使用mod 26。



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']
print('write the message you want to decode?')
inputString = input()
inputString = inputString.lower()

transAlphabet = {}

def createDict(shift):
    for i in range(0,26):
        letter = alphabet[i]

        transAlphabet[letter]=alphabet[(i+shift)%26]


def encodeMessage(message):
    cypherText = ''
    for letter in message:
        if letter in transAlphabet:

            cypherText = cypherText + transAlphabet[letter]
        else:
            cypherText = cypherText + letter
    print(cypherText)

for i in range(0,26):
    createDict(i)
    encodeMessage(inputString)



[如果有人帮助我谢谢您,将非常有帮助!

我试图在不知道凯撒密码密钥的情况下,从用户那里解密文本。我可能大部分都理解,但是我不明白为什么他们在第8行使用mod 26字母表= ['a','b','c','d','e',...

python encryption key caesar-cipher
1个回答
1
投票

%26的模是使字母变成圆形,当您在z处时返回a

© www.soinside.com 2019 - 2024. All rights reserved.