Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 cipher
- encryptionKey = 16
- # The Caesar cipher for encrypting passwords
- 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. Delete password")
- print(" 7. Quit program")
- print("Please enter a number (1-7)")
- 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
- for password in passwords:
- if passwordToLookup == password[0]:
- unencryptedPassword = passwordEncrypt(password[1], -encryptionKey)
- print(unencryptedPassword)
- # 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)
- password=[website, encryptedPassword]
- passwords.append(password)
- # 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': # delete a password from the passwords array and saved file
- print("Which website do you want your password deleted?")
- for keyvalue in passwords:
- print(keyvalue[0])
- passwordToDelete = input()
- for idx, password in enumerate(passwords):
- if passwordToDelete == password[0]:
- del passwords[idx]
- savePasswordFile(passwords,passwordFileName)
- if choice == '7': # quit our program
- sys.exit()
- print()
- print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement