On 09/10/16 01:50, Linda Gray wrote:

> I am working on a homework assignment that has me creating a password saver
> using a ceasar cipher code.  I was provided the key to the cipher and two
> passwords.  I need to look up and decrypt the passwords 

Are you sure? That's very bad practice and never needed
in the real world.

The normal way to handle passwords is to encrypt them
and store the encryopted cersion. Then when the user
enters a password you encrypt that and compare it to
the stored encryption. If the two encrypted versions
are the same then the original passwords were the same.

So you should never need to see the plaintext
version of a password, that would be a bad
security hole.

So are you sure you need to decrypt the password?
Is that an explicit part of your assignment?


> just not doing anything that I can see.  I am getting the following error
> with the deleting a password.
> 
> Traceback (most recent call last):
>   File "C:/Users/lrgli/Desktop/Python Programs/Password test file.py", line
> 187, in <module>
>     passwords.remove (passwordToDelete)
> ValueError: list.remove(x): x not in list


> Here is my 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
> 
> 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(" 7. Delete a password")
>     print("Please enter a number (1-4 or 7)")
>     choice = input()
> 
>     if(choice == '1'): #Load the password list from a file
>         passwords = loadPasswordFile(passwordFileName)
> 

You dopn;t need parens areound the test in Python.

    if choice == '1':

works just fine.


>     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)):  #  This loops through my list
> of passwords

Its usually better to loop over the list directly:

for website,password in passwords:


>             if passwordToLookup in passwords [i][0]:  #this says what
> matches up the website you input with the website in the lists of
> passwords
>                   # print (passwords[i][1])  #this prints the encrypted 
> password

And this becomes

if passwordToLookup == website:
   print password


> 
>                    #The encryption key for the caesar cypher
>                    deencryptionKey=16
> 
>                    #Caesar Cypher Encryption
>                    def passwordunEncrypt (encryptedMessage, key):
> 
>                        #We will start with an empty string as our
> encryptedMessage
>                        unencryptedMessage = ''
> 
>                        #For each symbol in the unencryptedMessage we
> will add an encrypted symbol into the encryptedMessage
>                        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
> 

Put the algorithgm into a function for easier testing and readability.


>                            unencryptedMessage += chr(num)
>                        else:
>                             unencryptedMessage += symbol
> 
>                        return unencryptedMessage

return should throw an error since its not inside a function?
Looks like a cut n [aste error maybe?

>                        print(passwordunEncrypt(passwords[i][1], 16))
> 
> 
> 
> 
>     if(choice == '3'):
>         print("What website is this password for?")
>         website = input()
>         print("What is the password?")
>         unencryptedPassword = input()
> 
>         encryptedPassword = passwordEncrypt
> (unencryptedPassword,encryptionKey)  # This encrypts the password you
> just entered
> 
>         newwebsitepassword = [website, encryptedPassword]  # This
> creates my list of 2 items, website and password
> 
>         passwords.append(newwebsitepassword)  #This adds this to the
> password list
> 
> 
>     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()
> 
> 
>     if (choice == '7'):  #delete a password
>       print ("What website password would you like to delete?")
> 
>       for keyvalue in passwords:
>             print(keyvalue[0])
>       passwordToDelete = input()
> 
>       for i in range(len(passwords)):  #  This loops through my list
> of passwords
>             if passwordToDelete in passwords [i][0]:  #this says what
> matches up the website you input withthe website in the lists of
> passwords
>                passwords.remove (passwordToDelete)

This doesn't work because passwordtodelete is the
website entry, not the whole entry in passwords.
You need to delete passwords[i] or, if you want to
keep the web site and just delete the password
element, then delete passwords[i][1]


hth
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to