Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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:
1 = Encrypt Message
2 = Decrypt Message

')
    choice = int(choice)
    if choice == 1:
        text = input('Enter message:
')
        s = input('Enter encryption key number: ')
        s = int(s)
        encrypt(text,s)
        print ("Encrypted message: " + encrypt(text,s)+"
")
        selection()

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

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

selection()
question from:https://stackoverflow.com/questions/65651081/decrypt-non-alphabetical-characters-in-python-ceasar-cypher

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
286 views
Welcome To Ask or Share your Answers For Others

1 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:
1 = Encrypt Message
2 = Decrypt Message

')
        choice = int(choice)
        if choice == 1:
            plain_text = input('Enter message:
')
            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:
')
            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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...