我写的代码是一个Vignere密码加密程序,它使用关键字来加密消息。我写了这段代码,当我完成后,我运行它,它做了所有它应该做的,只是输出加密的消息。请参阅下面的代码,我们将非常感谢您的帮助:
ans = False
print(""" *****Hello. Welcome to the Vignère Cipher Encryption Program*****
***This program uses a keyword that is repeated until it
matches the same lenght of the message and then adds its
numerical value to the numerical value of the message and
outputs the encrypted message in alpha.
Please press:
E to Encrypt
D to Decrypt
or double tap enter to quit.
""")
ans=input("What would you like to do now???")
if ans == "E":
plaintext = input("Please enter a message to be encrypted: ").upper()
keyword = input("Please enter a keyword to be used to encrypt a message (alpha only): ").upper()
ciphered = " "
for i in range (len(plaintext)):
char = plaintext[i]
alphakeywordvalue = ord(keyword[i%len(keyword)]) - ord("A")+1
if char.isupper():
if ans == "E" :
value = ord(char) + alphakeywordvalue
if value > ord("Z"):
value -= 26
print ("Your encrypted text is:", ciphered)
elif ans == "D":
plaintext = input("Please enter a message to be dencrypted: ").upper()
keyword = input("Please enter a keyword to be used to dencrypt a message (alpha only(make sure that it is the same keyword used to encrypt the message)): ").upper()
ciphered = " "
for i in range (len(plaintext)):
char = plaintext[i]
alphakeywordvalue = ord(keyword[i%len(keyword)]) - ord("A")+1
if char.isupper():
if ans == "D" :
value = ord(char) - alphakeywordvalue
if value <ord("A"):
value += 26
ciphered += chr(value)
print ("Your decrypted text is:", ciphered)
这不是一种好的代码编写风格。很破旧,很难看。如果需要,您应该为各个部分创建方法,并创建一个单独的main()类的部分来与用户交互。发动机应该隐藏起来,车身应该抛光。把它们分开。是的,不要重复你自己
好吧,这是我重新编写的代码。重要部分包含注释。错误在后面解释。。在
就这样。现在您可以编写与用户或其他模块交互的代码。这是一个样品测试:-在
^{pr2}$输出如下:![enter image description here](https://i.stack.imgur.com/7cnvR.png)
现在您可以编辑这个方法来处理更多的情况,如超出ascii范围的字符等
它如何打印加密的消息,加密例程从不从空字符串
ciphered
更改。在ciphered
设置为空字符串,不会更改。在ans == "E"
因为你之前测试过它相关问题 更多 >
编程相关推荐