From e17f5ccb51db8d506bbf95fad8fe481e4396e611 Mon Sep 17 00:00:00 2001 From: Kamalakar Satapathi <103712713+Sgvkamalakar@users.noreply.github.com> Date: Mon, 23 Oct 2023 14:57:28 +0530 Subject: [PATCH] Create caesar_cipher_encoder__decoder.py --- python/caesar_cipher_encoder__decoder.py | 42 ++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 python/caesar_cipher_encoder__decoder.py diff --git a/python/caesar_cipher_encoder__decoder.py b/python/caesar_cipher_encoder__decoder.py new file mode 100644 index 0000000..c8ae0cf --- /dev/null +++ b/python/caesar_cipher_encoder__decoder.py @@ -0,0 +1,42 @@ +def encrypt(text, n): + encrypted = "" + for char in text: + if char.isupper(): + c = ord(char) + (n % 26) + if c > ord('Z'): + c -= 26 + elif char.islower(): + c = ord(char) + (n % 26) + if c > ord('z'): + c -= 26 + else: + c = ord(char) + 1 + encrypted += chr(c) + return encrypted + +def decrypt(text, n): + decrypted = "" + for char in text: + if char.isupper(): + c = ord(char) - (n % 26) + if c < ord('A'): + c += 26 + elif char.islower(): + c = ord(char) - (n % 26) + if c < ord('a'): + c += 26 + else: + c = ord(char) - 1 + decrypted += chr(c) + return decrypted + +def main(): + text = input("Enter a string: ") + n = int(input("Enter key: ")) + encrypted_string = encrypt(text, n) + print("\nEncrypted text:", encrypted_string) + decrypted_string = decrypt(encrypted_string, n) + print("Decrypted text:", decrypted_string) + +if __name__ == "__main__": + main()