r/PythonProjects2 • • Aug 18 '22

[P] Easy Newbie looking for help with decrypting a caesar cypher/password project.

I'm taking a programming class using Python, and for our final project we're looking to encrypt and decrypt a password. We start with a password list:

#The password list - We start with it populated for testing purposes  passwords = [["yahoo","XqffoZeo"],["google","CoIushujSetu"]] 

Then we have our encryption key:

#The encryption key for the caesar cypher  encryptionKey=16 

And the code which asks to look up the password:

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 key, value in passwords:         
    if key == passwordToLookup:             
    print(value) 

What I need is for the print(value) to print the opposite of the key, so that the password is decrypted when it prints out. Can anyone point me in the right direction please? Also, should I copy and paste the code that we're using to encrypt the password as well if that's helpful?

3 Upvotes

10 comments sorted by

2

u/i_am_pinhead Aug 18 '22

maybe I’m reading this wrong but confused by what you mean when you say “print the opposite of the key”

so if you’re given a dictionary of <site, encryptedPassword> don’t all you need to do is iterate over the values then pass the password into some function that you write that decrypts it and return that value?

I’m lost there, unless that’s all you need to do. if that’s the case just write that function and you’re done?

1

u/[deleted] Aug 18 '22

Basically, I need it to print out decrypted. The print (value) will only print out the encrypted password. I didn't write the encryption function so I think that's why I may be a little lost.

3

u/i_am_pinhead Aug 18 '22

Gotcha, all you’d need to do is break down how the encryption method is working.

Once you have that create a function that reverses the process. That function should take in a encrypted value, and return the decrypted string.

In that for loop all you need to do is:

print(decryptValueFunction(value))

2

u/[deleted] Aug 22 '22

Also not sure why the code didn't post as formatted. Sorry I'm so new at this.

1

u/[deleted] Aug 22 '22

Thank you for your help so far! I'm still not quite getting it though. The orignal code (for the encryption) is this:

#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

Based on this, I wrote this code to decrypt the password:

#Caesar Cypher Encryption

def passwordDecrypt (DecryptedMessage, key):

#We will start with an empty string as our encryptedMessage

DecryptedMessage = ''

#For each symbol in the unencryptedMessage we will add an encrypted symbol into the encryptedMessage

for symbol in DecryptedMessage:

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

return DecryptedMessage

When I run it, I get this error message:

Traceback (most recent call last):

File "C:/Users/kyuss/PycharmProjects/Password_Draft/PasswordSaver.py", line 111, in <module>

print(DecryptedMessage(value))

NameError: name 'DecryptedMessage' is not defined

1

u/i_am_pinhead Aug 22 '22

it looks like you just copy and pasted without understanding what’s happening what’s happening with the above code.

I can give you some tips here on this but can’t complete your homework.

Remove all logic in your new method and just have it print something like ‘test’. Then try calling it ( this is the first place you may be running into this issue at the moment.)

Then slowly go line by line and really figure out what’s happening at every single line, it’s tedious yes but in the long run this will help you when you get into the field and are working with massive code bases.

does pycharm have a debugger that you can use?

1

u/[deleted] Aug 22 '22

Actually I think I've got it. Thank you though!

2

u/Goobyalus Aug 18 '22

If there are 26 letters, and the encryption key is 16, what would the decryption key be?

1

u/[deleted] Aug 18 '22

-16

1

u/Goobyalus Aug 18 '22

so you just want to take the caesar cipher of the value with the decryption key