凯撒密码加密。字符移位不正确的一些问题

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

嘿,我正在尝试使用shift键对字符串进行加密。它运行完美,我唯一的问题是移动字符。例如hello,world!0,移位将为5。我希望它返回为olssv,dvysk!0基本上,只有字母会被加密。按键和数字等将不会转移。

keymax = 26
def text():
    print('What message(s) are you trying to encrypt?')
    return input()

def shift():
    key = 0;
    while True:
        print('Enter the key number (1-%s)' % (keymax))
        key = int(input())
        if (key >= 1 and key <= keymax):
            return key

def encrypt(string, shift):
    hidden = ''
    for char in string:
        if char == ' ':
            hidden = hidden + char
        elif  char.isupper():
            hidden = hidden + chr((ord(char) + shift - 65) % 26 + 65)
        else:
          hidden = hidden + chr((ord(char) + shift - 97) % 26 + 97)

    return hidden

text = text()
s = shift()
print("original string: ", text)
print("after encryption: ", encrypt(text, s))

我对python还是很陌生,对我的理解不好。任何帮助将不胜感激!

python encryption caesar-cipher
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.