Advertisement
Guest User

Python Ceaser Cipher

a guest
Feb 22nd, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.73 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 cipher
  11. encryptionKey = 16
  12.  
  13. # The Caesar cipher for encrypting passwords
  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.  
  43. def loadPasswordFile(fileName):
  44.  
  45.     with open(fileName, newline='') as csvfile:
  46.         passwordreader = csv.reader(csvfile)
  47.         passwordList = list(passwordreader)
  48.  
  49.     return passwordList
  50.  
  51. def savePasswordFile(passwordList, fileName):
  52.  
  53.     with open(fileName, 'w+', newline='') as csvfile:
  54.         passwordwriter = csv.writer(csvfile)
  55.         passwordwriter.writerows(passwordList)
  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. Delete password")
  66.     print(" 7. Quit program")
  67.     print("Please enter a number (1-7)")
  68.     choice = input()
  69.  
  70.     if choice == '1':  # Load the password list from a file
  71.         passwords = loadPasswordFile(passwordFileName)
  72.  
  73.     if choice == '2':  # Lookup at password
  74.         print("Which website do you want to lookup the password for?")
  75.         for keyvalue in passwords:
  76.             print(keyvalue[0])
  77.         passwordToLookup = input()
  78.  
  79.         # YOUR CODE HERE
  80.         for password in passwords:
  81.             if passwordToLookup == password[0]:
  82.                 unencryptedPassword = passwordEncrypt(password[1], -encryptionKey)
  83.         print(unencryptedPassword)
  84.         # YOUR CODE HERE
  85.  
  86.     if choice == '3':
  87.         print("What website is this password for?")
  88.         website = input()
  89.         print("What is the password?")
  90.         unencryptedPassword = input()
  91.  
  92.         # YOUR CODE HERE
  93.         encryptedPassword = passwordEncrypt(unencryptedPassword,encryptionKey)
  94.         password=[website, encryptedPassword]
  95.         passwords.append(password)
  96.         # YOUR CODE HERE
  97.  
  98.     if choice == '4': # Save the passwords to a file
  99.         savePasswordFile(passwords,passwordFileName)
  100.  
  101.     if choice == '5':  # print out the password list
  102.         for keyvalue in passwords:
  103.             print(', '.join(keyvalue))
  104.            
  105.     if choice == '6': # delete a password from the passwords array and saved file
  106.         print("Which website do you want your password deleted?")
  107.         for keyvalue in passwords:
  108.             print(keyvalue[0])
  109.         passwordToDelete = input()
  110.         for idx, password in enumerate(passwords):
  111.             if passwordToDelete == password[0]:
  112.                 del passwords[idx]
  113.                 savePasswordFile(passwords,passwordFileName)
  114.  
  115.  
  116.     if choice == '7':  # quit our program
  117.         sys.exit()
  118.  
  119. print()
  120. print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement