繁体   English   中英

如何使凯撒密码与 Python 中有空格的输入一起使用

[英]How to make a caesar cipher work with input that has spaces in Python

x = input('Please Enter a Sentence: ')
y = input('Please Enter a Number: ')
y = int(y)
g = list(x)
j = ['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']
limit = 25
for e in g:
    q = j.index(e)
    if (q+y) > 25:   
        oman = (q+y) % 26
        newg = j[oman]
        print(newg, end="")
    elif (q+y) <= 25:
        newg = j[q+y]
        print(newg, end="")

我必须用python写一个凯撒密码来做作业。 (凯撒密码在短语中移动字母以破译某些东西。)

我的程序应该将一个句子作为一个数字作为输入,并将句子中的每个字母按输入数字移动。 它适用于没有空格的短语,但我需要它来处理空格。 此外,该程序不需要考虑标点符号。

如果有人能给我一些关于如何做到这一点的指示,那将非常有帮助。

我尝试在字母表变量中放置一个空格项,但随后关闭了班次。

你可以这样做

def Cesar(input_string,key):
    new_string=""
    for i in range(len(input_string)):
       if input_string[i].isalpha():
           curr_char=input_string[i].lower()
           #do your shifting to curr_char with key and append to a new string.
       elif input_string[i]==' ':
           new_string+=' '
    return new_string

就这么简单。

您几乎已经构想出一种实际使用 Caser 密钥并加密消息的算法,因此我将仅提及如何确保程序不会遗漏空格并按照您的要求忽略标点符号。

 encrypt=str(input("Enter a message"))
 b=""
    for i in encrypt:
     if i==chr(32):
         b+=i
     #32 is the ASCII number for a space(" "), chr is a function which converts a character to its ASCII number
     #after carry on with the actual algorithm to encrypt letters

要忽略标点符号,您可能需要执行以下操作:

if i in ("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"):

在上述之后,然后执行您的代码,这样程序将只处理来自 az 的字母,而不会处理其他任何事情。

我稍微更改了代码,使其更通用,并尝试为变量选择好的描述性名称。 您现在可以为ceasar使用不同的列表 - 任何不在其中的内容都将完全相同。

与此相关的是,如果您喜欢密码学,您可以阅读NSA 的旧密码分析手册

那里有很多关于密码的有趣信息,有关字母频率分析的信息将允许您编写一个程序来破解 Ceasar 密码,而不必先尝试所有 25 个班次!

sentence = input('Please Enter a Sentence: ')
number   = input('Please Enter a Number: ')

shift   = int(number)
letters = list(sentence)
cipher  = '' 

ceasar = ['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']

for letter in letters:
    if letter in ceasar:
        oldindex = ceasar.index(letter)
        newindex = (oldindex + shift) % len(ceasar)
        newletter = ceasar[newindex]
    else:
        newletter = letter

    print(letter, end="")
    cipher += newletter

如果您不想存储带有字母表的列表......(也处理西班牙字符)

def cesar(text, key):
    if key == 0:
        return text
    else:
        text = text.replace("á", chr(97)).replace("é", "e").replace("í", "i").replace("ó", "o").replace("ú", "u")\
        .replace("Á", "A").replace("É", "E").replace("Í", "I").replace("Ó", "O").replace("Ú", "U")\
        .replace("ñ", "n").replace("Ñ", "N")
        cipher = ""
        for x in range(len(text)):
            if text[x].isalpha():  # only shifting letters
                if text[x].islower():  # lowercase
                    cipher += chr((ord(text[x]) - 97 + key) % 26 + 97)
                else:  # uppercase
                    cipher += chr((ord(text[x]) - 65 + key) % 26 + 65)
            else:
                cipher += text[x]
        return cipher

函数ord(char)给你一个字符的数字(AZ 是 65-...,az 是 97-...),函数chr(int)给你一个数字的字符(与另一个)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM