Skip to content
Advertisement

decrypt non alphabetical characters in python ceasar cypher

I found online a working script to try on ceaser cypher, I modified it a bit to be able to decrypt and it’s working well for both sides except one detail:

whenever it’s a special character (non alphabetical) such as: , . ! () ect… it encrypts it like a letter (fine and normal I guess) but once decrypted, these special characters are transformed into a letter, of course it’s still kinda readable, but not fully decrypted as it should.

I’m pretty new to encryption, I’ve looked arround a bit but don’t seem to find any solutions so any insight will be helpful

here is the script:

def encrypt(text,s):
result = ""

for i in range(len(text)):
    char = text[i]
    
    if (char.isupper()):
        result += chr((ord(char) + s-65) % 26 + 65)
  
    else:
        result += chr((ord(char) + s - 97) % 26 + 97)
return result



def decrypt(text,s):
result = ""

for i in range(len(text)):
    char = text[i]
    
    if (char.isupper()):
        result += chr((ord(char) + s-65) % 26 + 65)
  
    else:
        result += chr((ord(char) + s - 97) % 26 + 97)
return result



def selection():
    choice = input('Enter selection:n1 = Encrypt Messagen2 = Decrypt Messagenn')
    choice = int(choice)
    if choice == 1:
        text = input('Enter message:n')
        s = input('Enter encryption key number: ')
        s = int(s)
        encrypt(text,s)
        print ("Encrypted message: " + encrypt(text,s)+"n")
        selection()

    elif choice == 2:
       text = input('Enter encrypted message:n')
       s = input('Enter decryption key number: ')
       s = int(s)
       decrypt(text,s)
       print ("Decrypted message: " + encrypt(text,s)+"n")
       selection()

   else:
       print("Error! Enter 1 (Message Encryption) or 2 (Message Decryption)")
       selection()

selection()

Advertisement

Answer

this is happening because your encrption and decription “know” just alphabetical chars. your script isn’t generic to all computer chars. try this code for ceaser cypher

def encrypt(plain_text, key):
    return ''.join([chr(ord(i) + key) for i in plain_text])



def decrypt(encryped_text, key):
    return encrypt(encryped_text, -key)


def selection():
    while True:
        choice = input('Enter selection:n1 = Encrypt Messagen2 = Decrypt Messagenn')
        choice = int(choice)
        if choice == 1:
            plain_text = input('Enter message:n')
            key = int(input('Enter encryption key number: '))
            encrypted_text = encrypt(plain_text, key)
            print(f"Encrypted message: {encrypted_text}")
        elif choice == 2:
            encrypted_text = input('Enter encrypted message:n')
            key = int(input('Enter decryption key number: '))
            plain_text = decrypt(encrypted_text, key)
            print(f"Decrypted message: {plain_text}")
        else:
            print("Error! Enter 1 (Message Encryption) or 2 (Message Decryption)")

selection()

I also changed the decrypt function to be the opposite of encryptin because ceaser cypher is Symmetric encryption, in other words, the user give the encrypt and decrypt the same key in both sides.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement