简体   繁体   English

我正在为我的学校项目构建一个凯撒密码,并且想知道解密部分将如何工作

[英]I am building a caesar cipher for my school project and would like to know how the decryption part would work

I am writing in Python 3.4 and have so far programmed this: 我正在用Python 3.4编写程序,到目前为止已对此进行了编程:

plaintext = ""
print ("Do you want to encrypt or decrypt a phrase?")
answer = input("Type e for encrypt or d for decrypt and hit 'Enter'.").lower()
if answer == ("e") or answer == ("encrypt"):
    plaintext = input("Enter the phrase you would like to encrypt:")
else:
    print("I don't understand")

shift = int(input("How many shifts would you like to make?")

alphabet = "abcdefghijklmnopqrstuvwxyz"
shiftedAlphabet = "jklmnopqrstuvwxyzabcdefghi"
ciphertext = ""


for eachletter in plaintext:
    position = alphabet.index(eachletter)
    shiftedLetter = shiftedAlphabet[position]
    ciphertext = ciphertext + shiftedLetter


print(ciphertext)


if answer == ("d") or answer == ("decrypt")
    ciphertext = input("Enter the phrase you would like to decrypt:"
else:
    print("mmmm okay")

shiftedAlphabet -  

I don't how to program the decryption part (please note: I would like the code relatively simple and would like it to be similar to that above). 我不对解密部分进行编程(请注意:我希望代码相对简单,并且希望与上面的代码相似)。

Caesar cyphers are symmetric - you just need to apply the same logic backwards - find the index in the ciphered text and get that index from the plain alphabet: 凯撒密码是对称的-您只需要向后应用相同的逻辑-在密文中找到索引,然后从普通字母中获取该索引:

plain = ''
for eachletter in cipertext:
    position = shiftedAlphabet.index(eachletter)
    shiftedLetter = alphabet[position]
    plain = plain + shiftedLetter

So there are a couple issues with the code you've posted already. 因此,您已经发布的代码存在一些问题。 There are some minor syntax errors I can overlook, but there are also some logical errors. 我可以忽略一些小语法错误,但是也有一些逻辑错误。

Your second prompt to the user asks how many shifts they would like to make, yet the shifted alphabet you use for your program always shifts from 'a' to 'j' . 给用户的第二个提示询问他们想要进行多少次转换,但是您在程序中使用的转换后的字母始终从'a''j' What happens if the use enters a shift that's not 9 ? 如果使用者输入的班次不是9 ,会发生什么?

On another note, what happens when the input plaintext has a character that is not in the alphabet? 另一个需要注意的是,当输入的纯文本具有一个不在字母表中的字符时,会发生什么? When you do .index(eachletter), it will return -1. 当您执行.index(eachletter)时,它将返回-1。 How will that cause your cipher to behave? 这将如何导致密码行为?

Finally, it appears your instructor would like to be able to press 'e' or 'd' at any time to encrypt or decrypt, however right now if a user tries to decrypt at the beginning, I don't understand is printed. 最后,看来您的讲师希望能够随时按'e''d'进行加密或解密,但是现在如果用户开始尝试解密时, I don't understand打印出来了。 Same problem with decrypt at the end. 最后还有解密的同样问题。 Also, you will probably need to ask for a shift when you decrypt as well. 同样,解密时您可能还需要请求转移。

My last piece of advice is to think about what a Caesar cipher is. 我的最后一条建议是考虑什么是凯撒密码。 You merely rotate letters. 您只需旋转字母。 If the input is a cat and the shift is 2 , you get c ecv . 如果输入是a cat ,并且班次是2 ,则得到c ecv To decrypt, you simply need to rotate each letter backwards the same amount, 2 . 要解密,您只需要将每个字母向后旋转相同的数量2

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

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