Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.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 我的程序运行,但它没有';t输出加密的消息_Python_Python 3.x_Encryption_Vigenere - Fatal编程技术网

Python 我的程序运行,但它没有';t输出加密的消息

Python 我的程序运行,但它没有';t输出加密的消息,python,python-3.x,encryption,vigenere,Python,Python 3.x,Encryption,Vigenere,我写的代码是一个Vignere密码加密程序,它使用关键字对消息进行加密。我写了这段代码,当我完成它时,我运行了它,它做了它应该做的一切,但输出了加密的消息。请参阅下面的我的代码,我们衷心感谢您的帮助: ans = False print(""" *****Hello. Welcome to the Vignère Cipher Encryption Program***** ***This program uses a keyword that is repeated until

我写的代码是一个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)
ans=False
打印(“”******您好。欢迎使用Vignère密码加密程序*****
***该程序使用一个重复的关键字,直到
匹配消息的相同长度,然后添加其
数字值到消息的数字值,以及
以alpha格式输出加密消息。
请按:
加密
D解密
或者双击enter退出。
""")
ans=输入(“您现在想做什么?”)
如果ans==“E”:
明文=输入(“请输入要加密的消息:”).upper()
关键字=输入(“请输入用于加密邮件的关键字(仅限alpha):”。upper()
加密=“”
对于范围内的i(len(纯文本)):
char=纯文本[i]
alphakeywordvalue=ord(关键字[i%len(关键字)])-ord(“A”)+1
如果char.isupper():
如果ans==“E”:
值=ord(字符)+字母关键字值
如果值>ord(“Z”):
值-=26
打印(“您的加密文本为:”,已加密)
elif ans==“D”:
明文=输入(“请输入要解密的消息:”).upper()
keyword=input(“请输入用于对邮件进行解密的关键字(仅限alpha(确保它与用于加密邮件的关键字相同)):”。upper()
加密=“”
对于范围内的i(len(纯文本)):
char=纯文本[i]
alphakeywordvalue=ord(关键字[i%len(关键字)])-ord(“A”)+1
如果char.isupper():
如果ans==“D”:
值=ord(字符)-字母关键字值

如果value它如何打印加密的消息,则加密例程不会从空字符串中更改
加密

    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)
  • 加密
    设置为空字符串,永远不会更改
  • 您总是知道char是大写的,因为您将所有纯文本都设置为大写()
  • 你知道ans==“E”
  • ,因为你之前测试过它
  • 这个print()到目前为止是缩进的,它每次通过循环都会尝试打印

  • 这不是一种编写代码的好风格。非常简陋,难以阅读。如果需要,您应该为各个部分创建方法,并创建一个单独的类似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
    
    以下是输出:


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

    在加密情况下,
    加密的
    永远不会被修改,其值始终为
    。您的打印在for循环内。如果ans==“E”(内部的一个是无用的),则无需测试两次。