In: Computer Science
I used this code for my first draft assignment. My teacher told me not to add a decrypt function. I have the variable encryptionKey holding 16. I can simply negate encryptionKey with the - in front. Then I make one key change and all the code still works properly. How do I adjust this code?
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 #Caesar Cypher Decryption def passwordDecrypt (encryptedMessage, key): # We will start with an empty string as our unencryptedMessage unEncryptedMessage = '' # For each symbol in the encryptedMessage we will add an unencrypted symbol into the unencryptedMessage for symbol in encryptedMessage: 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 unEncryptedMessage += chr(num) else: unEncryptedMessage += symbol return unEncryptedMessage 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() # Iterate through password list # Match it with user input # If matches, save password in a variable and break from loop # Print password # Decrypt password and print it as well for i in range(len(passwords)): if passwords[i][0]==passwordToLookup: pwd=passwords[i][1] break print(pwd) decryptedPassword=passwordDecrypt(pwd,encryptionKey) print(decryptedPassword) if (choice == '3'): print("What website is this password for?") website = input() print("What is the password?") unencryptedPassword = input() # Encrypt entered password # Create a list with website name and passsword # Add this to existing passwords list encryptedPassword = passwordEncrypt(unencryptedPassword, encryptionKey) pwdList=[website,encryptedPassword] passwords.append(pwdList) 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()
I've corrected the code and it's working perfectly.
Code:
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
#had to add this item here for the decryption to work. When trying to add it below, it didn't work.
def passwordDecrypt (encryptedMessage, key):
decryptedMessage = ''
for symbol in encryptedMessage:
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
decryptedMessage += chr(num)
else:
decryptedMessage += symbol
print (decryptedMessage)
return decryptedMessage
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()
for i in range(len(passwords)):
if passwordToLookup == passwords[i][0]:
unencryptedMessage = passwords[i][1]
passwordDecrypt(unencryptedMessage, -16)
if(choice == '3'):
print("What website is this password for?")
website = input()
print("What is the password?")
unencryptedPassword = input()
encryptedPassword = passwordEncrypt(unencryptedPassword, encryptionKey)
passwords.append([website, encryptedPassword])
print (passwords)
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()
Code screenshots:
Output:
-------------------------END---------------------
Please give a thumbs up(upvote) if you liked the answer.