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
#1
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':
            passwordEncrypt(print(passwords[0][1]))
        elif passwordToLookup == 'google':
            passwordEncrypt(print(passwords[1][1]))
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.
Quote
#2
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.
Quote
#3
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.
Quote
#4
Do you mind providing more of your code?
Quote
#5
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
encryptionKey=16

#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)
        else:
            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)
        passwordwriter.writerows(passwordList)



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:
            print(keyvalue[0])
        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.
Quote
#6
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)
Quote

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)