Jun-20-2017, 10:01 PM
For a project for a coding class I'm taking, we've been told to complete the code for an encrypted password saver. For part of this, I need to search for a password within a list of passwords based on the website associated with the password, then print the password. The passwords are saved in the list as follows:
Originally, I set out to do this via linear search. This is the code I'm using for that:
The full code is below. Anything in between "YOUR CODE HERE" or "MY CODE" comments is my own code, the rest was provided by the instructor.
passwords = [["yahoo","XqffoZeo"],["google","CoIushujSetu"]]Essentially, it's a list filled with lists, organized [website, password].
Originally, I set out to do this via linear search. This is the code I'm using for that:
def linsearch(myItem, myList): found = False position = 0 while position < len(myList) and not found: if myList[position] == myItem: found = True position = position +1 return foundThe problem being that whenever I call linsearch, it always returns "false." I assume this is because I'm searching an encrypted list. How could I decrypt the list, search it, print the password, and then encrypt it again? I'm having a hard time figuring it out.
The full code is below. Anything in between "YOUR CODE HERE" or "MY CODE" comments is my own code, the rest was provided by the instructor.
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 #load password from file def loadPasswordFile(fileName): with open(fileName, newline='') as csvfile: passwordreader = csv.reader(csvfile) passwordList = list(passwordreader) return passwordList #save password to file def savePasswordFile(passwordList, fileName): with open(fileName, 'w+', newline='') as csvfile: passwordwriter = csv.writer(csvfile) passwordwriter.writerows(passwordList) # The below section of code is a function that performs a linear search when called ##### my code ##### def linsearch(myItem, myList): found = False position = 0 while position < len(myList) and not found: if myList[position] == myItem: found = True position = position +1 return found ##### my code ##### #menu 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 ###### print(linsearch(int(passwordToLookup), passwords)) ####### YOUR CODE HERE ###### if(choice == '3'): print("What website is this password for?") website = input() print("What is the password?") unencryptedPassword = input() ####### YOUR CODE HERE ###### encryptedPassword = passwordEncrypt(unencryptedPassword, encryptionKey) newPassword = [website, encryptedPassword] passwords.append(newPassword) ####### YOUR CODE HERE ###### if(choice == '4'): #Save the passwords to a file savePasswordFile(passwords,passwordFileName) if(choice == '5'): #print out the password list for keyvalue in passwords: print(', '.join(keyvalue)) if(choice == '6'): #quit our program sys.exit() print() print()