def caesar_encript(txt, shift):
chiper = ""
for i in range(len(txt)):
char = txt[i]
if char == " " :
chiper = ' '
elif (char.isupper()):
chiper = chr((ord(char) shift - 65) % 26 65)
elif (char.islower()):
chiper = chr((ord(char) shift - 97) % 26 97)
return chiper
def caesar_decript(chiper, shift):
return caesar_encript(chiper, -shift)
msg = 'Random Mesage, WOOOWW!'
cpr = caesar_encript(msg,4)
txt = caesar_decript(cpr,4)
print('plain text : ', txt)
print('chiper text : ', cpr)
I made a code for encryption but the output does not display special characters and I want special characters to be displayed without encryption
CodePudding user response:
A crude solution would be to replace this
if char == " " :
chiper = ' '
with this
if not char.isalpha():
chiper = char
CodePudding user response:
The special characters are not considered because they're also being encrypted. This approach places an if condition before that where isalnum()
decides only alpha numeric characters to become encrypted.
def caesar_encript(txt, shift):
chiper = ""
for i in range(len(txt)):
char = txt[i]
if not char.isalnum():
chiper =char
else:
if (char.isupper()):
chiper = chr((ord(char) shift - 65) % 26 65)
elif (char.islower()):
chiper = chr((ord(char) shift - 97) % 26 97)
Output
plain text : Random Mesage, WOOOWW!
chiper text : Verhsq Qiweki, ASSSAA!