我的程序运行但它不输出加密消息

时间:2015-06-11 07:10:10

标签: python python-3.x encryption vigenere

我编写的代码是一个Vignere Cipher加密程序,它使用关键字来加密消息。我写了这段代码,当我完成它时,我运行它,它完成了所有它应该但输出加密的消息。请参阅下面的代码,感谢您的任何帮助:

    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)

2 个答案:

答案 0 :(得分:3)

这不是编写代码的好方式。非常破旧,难以阅读。您应该为各个部分创建方法,并创建一个单独的main()之类的部分,以便在需要时与用户进行交互。发动机应隐藏,车身应抛光。将它们分开。是的,不要重复自己

嗯,这是我重新编写的代码。重要部分包含注释。错误在它之后解释..

def encrypt(message, key, direction='E'):
    # Look here. There are three arguments. Third one takes 'E' to encrypt
    # and anything else to decrypt. You can modify to handle more cases

    ciphered = "" # Initialize. You did it almost well
    for i in range (len(message)):
        char = message[i]
        alphakeywordvalue = ord(key[i%len(key)]) - ord("A")+1 # Perfect. We took the key
        if direction=='E': # To encrypt
            value = ord(char) + alphakeywordvalue 
        else: # To decrypt
            value = ord(char) - alphakeywordvalue 
        ciphered += chr(value) # chr is the inverse of ord. It gets the character back
        # You missed this line
    return ciphered

那就是它。现在,您可以编写代码以与用户或其他模块进行交互。这是一个样本测试: -

message = "Hello World"
key = "abc"
print "ORIGINAL  : "+message

encoded_message = encrypt(message, key, 'E')
print "ENCRYPTED : "+encoded_message

plain_message = encrypt(encoded_message, key, 'D')
print "DECRYPTED : "+plain_message

这是输出: enter image description here

现在,您可以编辑此方法以处理更多情况,例如ascii范围字符等。

答案 1 :(得分:2)

如何打印加密邮件,加密例程永远不会从空字符串中更改ciphered

    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()
1)->    ciphered = " "
        for i in range (len(plaintext)):
            char = plaintext[i]
            alphakeywordvalue = ord(keyword[i%len(keyword)]) - ord("A")+1
2)->        if char.isupper():
3)->            if ans == "E" :
                    value = ord(char) + alphakeywordvalue 
                    if value > ord("Z"): 
                        value -= 26
4)->                    print ("Your encrypted text is:", ciphered)
  1. ciphered设置为空字符串,永远不会更改。
  2. 你总是知道char是上层的,因为你把所有的明文都写成了上层()
  3. 您知道ans == "E",因为您之前已经过测试
  4. 此print()到目前为止缩进,它试图通过循环每次打印