Example program for caesar cipher encryption method:
def encrypt(text,s):
result = ""
text=text.replace(" ","")
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
text =input("Enter the message:")
s = int(input("Enter the shift code:"))
print ("Text : " + text)
print ("Shift : " + str(s))
print ("Cipher: " + encrypt(text,s))
Sample Output:
Enter the message:Innovative Codes Academy
Enter the shift code:5
Text : Innovative Codes Academy
Shift : 5
Cipher: NsstafynajHtijxFhfijrd
Example program for caesar cipher decryption method:
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
text =input("Enter the decrypt message:")
s = int(input("Enter the shift code:"))
print ("Text : " + text)
print ("Shift : " + str(s))
print ("Cipher: " + decrypt(text,s))
Sample Output for caesar cipher decryption:
Enter the decrypt message:NsstafynajHtijxFhfijrd
Enter the shift code:5
Text : NsstafynajHtijxFhfijrd
Shift : 5
Cipher: InnovativeCodesAcademy
Any queries leave a comment please and thanks for reading this post.