Ex 2 Cns - 1022
Ex 2 Cns - 1022
Aim:
Description:
The Caesar Cipher is one of the simplest and oldest methods of encrypting messages, named
after Julius Caesar, who reportedly used it to protect his military communications. This technique
involves shifting the letters of the alphabet by a fixed number of places. For example, with a shift
of three, the letter ‘A’ becomes ‘D’, ‘B’ becomes ‘E’, and so on. Despite its simplicity, the Caesar
Cipher formed the groundwork for modern cryptographic techniques. In this article, we’ll explore
how the Caesar Cipher works, its significance, and its impact on the development of cryptography
with its advantages and disadvantages.
Program:
def caesar_cipher(text,shift,mode="encrypt"):
result=""
if mode=="decrypt":
shift=-shift
for char in text:
if char.isalpha():
ascil_offset=65 if char.isupper() else 97
shifted_char=chr(((ord(char) - ascil_offset+ shift) %26) + ascil_offset)
result+= shifted_char
else:
result+= char
return result
message="HELLO, WORLD!"
shift_value=3
encrypted_message=caesar_cipher(message,shift_value,mode="encrypt")
print("Encrypted:",encrypted_message)
1
23DC2055 – Cryptography and Network Security lab URK22AI1022
decrypted_message=caesar_cipher(encrypted_message, shift_value,mode="decrypt")
print("Decrypted:",decrypted_message)
Output
Encrypted: KHOOR, ZRUOG!
Decrypted: HELLO, WORLD!
Result
The above programs are verified and executed successfully.