Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
1,030
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. import csv
  2. import sys
  3.  
  4. #The password list - We start with it populated for testing purposes
  5. passwords = [["yahoo","XqffoZeo"],["google","CoIushujSetu"]]
  6.  
  7. #The password file name to store the passwords to
  8. passwordFileName = "samplePasswordFile"
  9.  
  10. #The encryption key for the caesar cypher
  11. encryptionKey=16
  12.  
  13. #Caesar Cypher Encryption
  14. def passwordEncrypt (unencryptedMessage, key):
  15.  
  16. #We will start with an empty string as our encryptedMessage
  17. encryptedMessage = ''
  18.  
  19. #For each symbol in the unencryptedMessage we will add an encrypted symbol into the encryptedMessage
  20. for symbol in unencryptedMessage:
  21. if symbol.isalpha():
  22. num = ord(symbol)
  23. num += key
  24.  
  25. if symbol.isupper():
  26. if num > ord('Z'):
  27. num -= 26
  28. elif num < ord('A'):
  29. num += 26
  30. elif symbol.islower():
  31. if num > ord('z'):
  32. num -= 26
  33. elif num < ord('a'):
  34. num += 26
  35.  
  36. encryptedMessage += chr(num)
  37. else:
  38. encryptedMessage += symbol
  39.  
  40. return encryptedMessage
  41.  
  42. def loadPasswordFile(fileName):
  43.  
  44. with open(fileName, newline='') as csvfile:
  45. passwordreader = csv.reader(csvfile)
  46. passwordList = list(passwordreader)
  47.  
  48. return passwordList
  49.  
  50. def savePasswordFile(passwordList, fileName):
  51.  
  52. with open(fileName, 'w+', newline='') as csvfile:
  53. passwordwriter = csv.writer(csvfile)
  54. passwordwriter.writerows(passwordList)
  55.  
  56.  
  57.  
  58. while True:
  59. print("What would you like to do:")
  60. print(" 1. Open password file")
  61. print(" 2. Lookup a password")
  62. print(" 3. Add a password")
  63. print(" 4. Save password file")
  64. print(" 5. Print the encrypted password list (for testing)")
  65. print(" 6. Quit program")
  66. print("Please enter a number (1-4)")
  67. choice = input()
  68.  
  69. if(choice == '1'): #Load the password list from a file
  70. passwords = loadPasswordFile(passwordFileName)
  71.  
  72. if(choice == '2'): #Lookup at password
  73. print("Which website do you want to lookup the password for?")
  74. for keyvalue in passwords:
  75. print(keyvalue[0])
  76. passwordToLookup = input()
  77.  
  78. for i in range(len(passwords)):
  79. if passwords[i][0] == passwordToLookup:
  80. print(passwords[passwords.index(passwordToLookup)+1])
  81. else:
  82. print("Password not in vault!")
  83.  
  84.  
  85. # if passwordToLookup in passwords: #Step 2
  86. # print([passwords.index(passwordToLookup)+1])
  87.  
  88.  
  89. #Step 3
  90.  
  91. #end my code
  92.  
  93. ####### YOUR CODE HERE ######
  94. #You will need to find the password that matches the website
  95. #You will then need to decrypt the password
  96.  
  97. #
  98. #1. Create a loop that goes through each item in the password list
  99. # You can consult the reading on lists in Week 5 for ways to loop through a list
  100. #
  101. #2. Check if the name is found. To index a list of lists you use 2 square backet sets
  102. # So passwords[0][1] would mean for the first item in the list get it's 2nd item (remember, lists start at 0)
  103. # So this would be 'XqffoZeo' in the password list given what is predefined at the top of the page.
  104. # If you created a loop using the syntax described in step 1, then i is your 'iterator' in the list so you
  105. # will want to use i in your first set of brackets.
  106. #
  107. #3. If the name is found then decrypt it. Decrypting is that exact reverse operation from encrypting. Take a look at the
  108. # caesar cypher lecture as a reference. You do not need to write your own decryption function, you can reuse passwordEncrypt
  109. #
  110. # Write the above one step at a time. By this I mean, write step 1... but in your loop print out every item in the list
  111. # for testing purposes. Then write step 2, and print out the password but not decrypted. Then write step 3. This way
  112. # you can test easily along the way.
  113.  
  114.  
  115. ####### YOUR CODE HERE ######
  116.  
  117.  
  118. if(choice == '3'):
  119. print("What website is this password for?")
  120. website = input()
  121. print("What is the password?")
  122. unencryptedPassword = input()
  123.  
  124. ####### YOUR CODE HERE ######
  125.  
  126. #Step 1
  127. encryptedPassword = passwordEncrypt(unencryptedPassword,encryptionKey)
  128. #Step 2
  129. passwords = [["yahoo","XqffoZeo"],["google","CoIushujSetu"]]
  130. #Step 3
  131. passwords.append([website,encryptedPassword])
  132.  
  133. #You will need to encrypt the password and store it in the list of passwords
  134.  
  135. #The encryption function is already written for you
  136. #Step 1: You can say encryptedPassword = passwordEncrypt(unencryptedPassword,encryptionKey)]
  137. #the encryptionKey variable is defined already as 16, don't change this
  138. #Step 2: create a list of size 2, first item the website name and the second item the password.
  139. #Step 3: append the list from Step 2 to the password list
  140.  
  141.  
  142. ####### YOUR CODE HERE ######
  143.  
  144. if(choice == '4'): #Save the passwords to a file
  145. savePasswordFile(passwords,passwordFileName)
  146.  
  147.  
  148. if(choice == '5'): #print out the password list
  149. for keyvalue in passwords:
  150. print(', '.join(keyvalue))
  151.  
  152. if(choice == '6'): #quit our program
  153. sys.exit()
  154.  
  155. print()
  156. print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement