My program runs but it doesn't output an Encrypted Message

260 Views Asked by At

The code I wrote is a Vignere Cipher encryption program that uses a keyword to encrypt a message. I wrote this code and when I finished it I ran it and it did all it was supposed to but output the encrypted message. See my code below, any help is gratefully received:

    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

There are 2 best solutions below

0
On

How could it print the encrypted message, the encryption routine never changes ciphered from an empty string.

    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 set to empty string, is never changed.
  2. You always know char is upper, because you made all the plaintext upper()
  3. You know ans == "E" because you tested it earlier
  4. This print() is so far indented it tries to print every time through the loop
0
On

This is not a good style of writing code. Very shabby and hard to read. You should make methods for individual parts and create a separate main() like section to interact with the user, if required. Engine should be hidden, car body should be polished. Keep them separate. And yes, DO NOT REPEAT YOURSELF

Well, here's my re-written code. Important parts contain the comments. Errors are explained after it..

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

That's it. Now you can write the code to interact with user or other modules. Here's a sample test:-

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

Here's the output: enter image description here

Now you can edit this method to handle more cases like out of ascii range characters etc.