Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Caesar密码按关键字而不是数字的ASCII值进行移位_Python_Encryption_Ascii - Fatal编程技术网

Python Caesar密码按关键字而不是数字的ASCII值进行移位

Python Caesar密码按关键字而不是数字的ASCII值进行移位,python,encryption,ascii,Python,Encryption,Ascii,我被指派用python编写一个Caesar密码程序。我使用了一个数字来移动/加密消息,但现在我需要使用一个关键字。关键字的重复次数足以匹配明文消息的长度。将密钥短语的每个字母的字母值添加到明文消息的每个字母的字母值中,以生成加密文本 MAX_KEY_SIZE = 26 def getMode(): while True: print('Do you wish to encrypt or decrypt a message?') mode = input()

我被指派用python编写一个Caesar密码程序。我使用了一个数字来移动/加密消息,但现在我需要使用一个关键字。关键字的重复次数足以匹配明文消息的长度。将密钥短语的每个字母的字母值添加到明文消息的每个字母的字母值中,以生成加密文本

MAX_KEY_SIZE = 26
def getMode():
    while True:
        print('Do you wish to encrypt or decrypt a message?')
        mode = input().lower()
        if mode in 'encrypt e decrypt d'.split():
            return mode
        else:
            print('Enter either "encrypt" or "e" or "decrypt" or "d".')
def getMessage():
    print('Enter your message:')
    return input()
def getKey():
    key = 0
    while True:
        print('Enter the key number (1-%s)' % (MAX_KEY_SIZE))
        key = int(input())
        if (key >= 1 and key <= MAX_KEY_SIZE):
            return key
def getTranslatedMessage(mode, message, key):
    if mode[0] == 'd':
        key = -key
    translated = ''
    for symbol in message:
        if symbol.isalpha():
            num = ord(symbol)
            num += key
            if symbol.isupper():
                if num > ord('Z'):
                    num -= 26
                elif num < ord('A'):
                    num += 26
            elif symbol.islower():
                if num > ord('z'):
                    num -= 26
                elif num < ord('a'):
                    num += 26
            translated += chr(num)
        else:
            translated += symbol
    return translated
mode = getMode()
message = getMessage()
key = getKey()
print('Your translated text is:')
print(getTranslatedMessage(mode, message, key))
getMode()
getMessage()
getKey()
getTranslatedMessage(mode, message, key)
getTranslatedMessage(mode, message, key)
MAX\u KEY\u SIZE=26
def getMode():
尽管如此:
print('是否要加密或解密邮件?')
模式=输入()
如果模式为“加密”“解密d”“.split():
返回模式
其他:
打印('输入“encrypt”或“e”或“decrypt”或“d”。)
def getMessage():
打印('输入您的消息:')
返回输入()
def getKey():
键=0
尽管如此:
打印('输入密钥编号(1-%s)'(最大密钥大小))
key=int(输入())
如果(键>=1和键ord('Z'):
num-=26
elif numord('z'):
num-=26
elif num
要获取单词中所有字符的添加ASCII值(将单词转换为数字),此函数应起作用:

def word_to_num(word):
    word = str(word) #Check it is a string
    ascii_value = 0
    for i in word:
        ascii_value += ord(i) #You can use many operations here
    return ascii_value

在代码的开头定义此项,然后传入关键字以将其转换为数字值。然后,您可以将现有代码用于数字密码。

查看,这似乎非常接近您想要执行的操作。对于
num-=26
,最好使用
num%=26
,以防万一。