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