Steakbomb
Steakbomb

Reputation: 13

Encoding a password with special characters

I am in an into to Programming class and the assignment is in part to shift the letters of a valid password by +1 if it is a letter or number and -1 if it is a !@#$ special character. I have most of it working with my below code but I always get the wrong output for the special characters. If I use to the code of anything high like 128 then I get the wrong symbol.

I am using the code from an encryption program from the other week and slowly changing things but I feel like this is too involved for something simple

If I enter the password UMA@augusta2020 I need to get the output VNB?bvhvtub3131 but I either end up with a space, b, or wrong symbol when I change the code input between 26,64,96,128, etc.

I have updated the code to fix small errors

def main():
    print("This program validates and encrypts a password")
    print()
main()

# The Encryption Function
def cipher_encrypt(plain_text):
    encrypted = ""
    for c in plain_text:
        if c.isupper(): #check if it's an uppercase character
            c_index = ord(c) - ord('A')
            # shift the current character by key positions
            c_shifted = (c_index + 1) % 26 + ord('A')
            c_new = chr(c_shifted)
            encrypted += c_new
        elif c.islower(): #check if its a lowecase character
            c_index = ord(c) - ord('a') 
            c_shifted = (c_index + 1) % 26 + ord('a')
            c_new = chr(c_shifted)
            encrypted += c_new
        elif c.isdigit():
            # if it's a number,shift its value 
            c_new = (int(c) + 1) % 10
            encrypted += str(c_new)
        else:
            # if its neither alphabetical nor a number, -1
            c_shifted = (c_index - 1) % 128 + ord('a')
            c_new = chr(c_shifted)
            encrypted += c_new
    return encrypted

plain_text =input("Enter your Password: ")
print()
ciphertext = cipher_encrypt(plain_text)
print()
print("Your Password: ", plain_text)
print()
print("Your password is valid and encrypted it is: ", ciphertext)
print()

Upvotes: 1

Views: 833

Answers (1)

itprorh66
itprorh66

Reputation: 3288

Here is a much cleaner approach:

def cipher_encrypt(plain_text):
    alpha = 'abcdefghijklmnopqrstuvwxyz'
    digits = '0123456789'
    puncs = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
    encrypted_text = ''
    for c in plain_text:
        if c.lower() in alpha:
            c_index = alpha.index(c.lower())
            e_index = (c_index + 1)%len(alpha)
            if c == c.lower():
                encrypted_text += alpha[e_index]
            else:
                encrypted_text += alpha[e_index].upper()
        elif c in digits:
            c_index = digits.index(c)
            e_index = (c_index + 1)%len(digits)
            encrypted_text += digits[e_index]
        else:
            c_index = puncs.index(c)
            e_index = (c_index +len(puncs) - 1)%len(puncs)
            encrypted_text += puncs[e_index]
    return encrypted_text     

In this approach I deal with each caharcter in plain text by:

  1. Determining idf the char is part of alpha by making use of the c.lower() function to isolate alpha chars and then find an index modulo the len of alpha. Then I determine if I need an uppercase char or lower case in the encrypted text.
  2. I use the same modulo approach for digits, but don't have to worry about upper and lower, finally
  3. I find the index for the punctuations found in the plain_text

Upvotes: 1

Related Questions