Python Forum

Full Version: Password Encryption Help Python3
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to make a program that is given a password and encrypts it by shifting each value up by 3. For example, if you entered in the password: Ab1 you would get De4. I'm trying to make it to where if the input is Z or 9, the output loops back to the start. So the password for Z9 would be C3.
I know I need to have a loop in order to accomplish this, but I'm not sure which type of loop/where to put the loop.


Quote:
def caesar(text):
    ciphertext=""
    for char in text:
        if char.isupper():
            ciphertext += chr((ord(char)+3 -65)%+26+65)
        elif char.islower():
            ciphertext += chr((ord(char)+3-97)%26+97)
        elif char.isdigit():
            ciphertext += chr((ord(char)+3))
        else:
            ciphertext+=char
    return ciphertext
text = input("Enter a password: ")
print('Plain Text: ', text)
print('Cipher:', caesar(text))
Quote:
You have a calulation method that works for upper and lowercase; For digits you do something different. Maybe you should use the same method, but not with 26 . Paul
Another approach could be using constants provided by built-in string module  (including ascii upper and lowercases and digits). Combining these constants with % for indices one should achieve desired result.