From edb8718d5158c0ad9b047015ab6eb737268fa8ea Mon Sep 17 00:00:00 2001 From: Abhishek <55457501+Abhishek-Dobliyal@users.noreply.github.com> Date: Thu, 1 Oct 2020 19:29:12 +0530 Subject: [PATCH] Added Ceaser Cipher Algorithm --- ceaser_cipher.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 ceaser_cipher.py diff --git a/ceaser_cipher.py b/ceaser_cipher.py new file mode 100644 index 0000000..344fdd8 --- /dev/null +++ b/ceaser_cipher.py @@ -0,0 +1,29 @@ + +def ceaser_cipher(text: str, s: int) -> str: + """ Returns the input text in encrypted form + + >>> ceaser_cipher(Hello!@2world, 3) + Returns -> Khoor!@2zruog + + """ + result = "" + + for i in range(len(text)): # Traverse the text + char = text[i] + + # Encrypt According to the case + if (char.isupper()): + result += chr((ord(char) + s-65) % 26 + 65) + + elif (char.islower()): + result += chr((ord(char) + s - 97) % 26 + 97) + + else: # Do not encrypt non alpha characters + result += char + + return result + +if __name__ == "__main__": + txt = input("Enter text to Encrypt: ") + key = int(input("Enter Your Key: ")) + print("Encrypted Text:", ceaser_cipher(txt, key))