[Tutor] Decrypting a Password
Peter Otten
__peter__ at web.de
Sun Oct 9 05:47:19 EDT 2016
Linda Gray wrote:
> Hello,
>
> 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 and create a
> program to add a password and delete a password (options 2, 3 and 7). I
> had no problems adding a password. I am also having no problems looking
> up a password but am havving problems decrypting the password.
The good news is that when you have a function that correctly encrypts a
string using caesar's cipher you also have one that decrypts it -- because
its the same function. Let's try:
>>> 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
...
>>> encrypted = passwordEncrypt("Hello world!", 16)
>>> encrypted
'Xubbe mehbt!'
>>> passwordEncrypt(encrypted, -16)
'Hello world!'
That's your function (without the empty lines to allow pasting into the
interactive interpreter without syntax errors), and it seems to work.
> I can only
> get it to provide me the give, encrypted password and not the unencrypted
> one
The line
> print(passwordunEncrypt(passwords[i][1], 16))
is incorrectly indented making it part of the passwordunEncrypt() function.
Therefore it is never executed.
It's generally a good idea to put functions at the beginning of a script and
to define them on the module level rather than at some arbitrary point in a
control structure (even experts hardly ever do that).
> which is the one I need to provide. I am not getting any errors, it is
> 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
>
> Any assistance, guidance, pointers would be appreciated. Is my
> indentation wrong for the decryption, am I missing something connecting
> the two, etc.?
The list contains lists (the site/password pairs) and remove looks for the
password alone. A small example:
>>> items = [["foo", "bar"], ["ham", "spam"]]
>>> items.remove("foo")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
For the removal to succeed you need to provide the exact entry:
>>> items.remove(["foo", "bar"])
>>> items
[['ham', 'spam']]
A workaround would be to loop over the items
>>> items = [["foo", "bar"], ["ham", "spam"]]
>>> for i, item in enumerate(items):
... if item[0] == "foo":
... del items[i]
... break # required!
...
>>> items
[['ham', 'spam']]
but Python has an alternative that is better suited for the problem: the
dictionary:
>>> lookup = {"foo": "bar", "ham": "spam"}
>>> del lookup["foo"]
>>> lookup
{'ham': 'spam'}
> I feel like I am close. It is python version 3.
>
> Here is my code:
More information about the Tutor
mailing list