Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 No idea how to use the Caesar Cypher in my code
So in our homework assignment, most of the code is given to us, and we have to write a few lines. Initially it asks for us to look at the passwords lists and print out a password based on the input, no problem, then it says to unencrypt the password using the Caesar Cypher, and tell us to go back to the lesson on it. The problem is it only tells us how to BUILD a Caesar Cypher, not how USE it in code. As it stands, I am trying to figure out how to use the already setup Caesar Cypher in my code so the passwords that print, are decrypted. I am not sure how much of the code I need to give, so I will start with the least possible, and go from there.

        if passwordToLookup == 'yahoo':
        elif passwordToLookup == 'google':
This is just the code I have written, I hope it has enough context, but I just am not sure how to implement the cypher into my code other than using the passwordEncrypt in it. In passwordEncrypt is (unencryptMessage, key), I just need to know how to implement the Caesar Cypher, no more.
You already have the function written so use it like any other function:

# Function signature is passwordEncrypt(unencryptMessage,key) based on your post
cyphered = passwordEncrypt(passwords[0][1], key)
Now, the code above does not include the key so I just entered "key" as the argument. Calling print() as an argument in your function call will cause problems. print() is only used to send information to standard out, your console.
Right, ok I forgot about the argument, thank you.

So I plugged in what "key" is, which is encryptionKey, but when I plugged it in, and ran it, it gives me an error saying

TypeError: passwordEncrypt() missing 1 required positional argument: 'key'

Problem is, there is no specific definition for key anywhere but way further up, all it does now is print the password, then 16, which is the key, then before that the error.

This has left me even more confused.
Do you mind providing more of your code?
I can do the whole thing, but I don't know what "too much code" would be. I will put in the code from the top, to my code, and see if that helps.
import csv
import sys

#The password list - We start with it populated for testing purposes
passwords = [["yahoo","XqffoZeo"],["google","CoIushujSetu"]]

#The password file name to store the passwords to
passwordFileName = "samplePasswordFile"

#The encryption key for the caesar cypher

#Caesar Cypher Encryption
def passwordEncrypt (unencryptedMessage, key):

    #We will start with an empty string as our encryptedMessage
    encryptedMessage = ''

    #For each symbol in the unencryptedMessage we will add an encrypted symbol into the encryptedMessage
    for symbol in unencryptedMessage:
        if symbol.isalpha():
            num = ord(symbol)
            num += key

            if symbol.isupper():
                if num > ord('Z'):
                    num -= 26
                elif num < ord('A'):
                    num += 26
            elif symbol.islower():
                if num > ord('z'):
                    num -= 26
                elif num < ord('a'):
                    num += 26

            encryptedMessage += chr(num)
            encryptedMessage += symbol

    return encryptedMessage

def loadPasswordFile(fileName):

    with open(fileName, newline='') as csvfile:
        passwordreader = csv.reader(csvfile)
        passwordList = list(passwordreader)

    return passwordList

def savePasswordFile(passwordList, fileName):

    with open(fileName, 'w+', newline='') as csvfile:
        passwordwriter = csv.writer(csvfile)

while True:
    print("What would you like to do:")
    print(" 1. Open password file")
    print(" 2. Lookup a password")
    print(" 3. Add a password")
    print(" 4. Save password file")
    print(" 5. Print the encrypted password list (for testing)")
    print(" 6. Quit program")
    print("Please enter a number (1-4)")
    choice = input()

    if(choice == '1'): #Load the password list from a file
        passwords = loadPasswordFile(passwordFileName)

    if(choice == '2'): #Lookup at password
        print("Which website do you want to lookup the password for?")
        for keyvalue in passwords:
        passwordToLookup = input()

        ####### YOUR CODE HERE ######
        #You will need to find the password that matches the website
        #You will then need to decrypt the password
        #1. Create a loop that goes through each item in the password list
        #  You can consult the reading on lists in Week 5 for ways to loop through a list
        if passwordToLookup == 'yahoo':
            passwordEncrypt(print(passwords[0][1], encryptionKey))
        elif passwordToLookup == 'google':
            passwordEncrypt(print(passwords[1][1], encryptionKey))
I hope this isn't too much code for a post.
The problem is on the last few lines. This will not work:

if passwordToLookup == 'yahoo':
    passwordEncrypt(print(passwords[0][1], encryptionKey))
elif passwordToLookup == 'google':
    passwordEncrypt(print(passwords[1][1], encryptionKey))
When functions are chained like that, they are called and returned from the inside out. So, print() will print the password and key to your console first. Then, print() returns None to passwordEncrypt() because it does not have a return statement. passwordEncrypt() assigns None as the first argument and has no value for the second argument.

To do this, you need this instead:

if passwordToLookup == 'yahoo':
    passwordEncrypt(passwords[0][1], encryptionKey)
elif passwordToLookup == 'google':
    passwordEncrypt(passwords[1][1], encryptionKey)

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Caesar cipher Drone4four 19 2,089 Nov-11-2018, 04:07 AM
Last Post: nilamo
  Vigenere and Caesar Cipher sammy2938 1 2,683 Jul-29-2017, 01:32 PM
Last Post: DeaD_EyE

Forum Jump:

Users browsing this thread: 1 Guest(s)