Skip to content

Instantly share code, notes, and snippets.

@cypher-nullbyte
Created August 19, 2020 11:24
Show Gist options
  • Save cypher-nullbyte/58bbbe1312a411df49d5cff32f73513d to your computer and use it in GitHub Desktop.
Save cypher-nullbyte/58bbbe1312a411df49d5cff32f73513d to your computer and use it in GitHub Desktop.
Ceaser Cipher Encryption and Decryption in python | https://youtu.be/0T7wWlst4Io
'''
author: cYpHeR
Github: cypher-nullbyte
'''
# -----------------------
# We assume that our input text is only lowercase string of alphabets without spaced
# LET US START
def Ceaser_encrypt(text,s):
result="" # this is for storing end encryptedText
# Let us visit our input (text) alpha by alpha :)
for i in range(len(text)):
char=text[i]
result+=chr((ord(char)+s-97)%26+97) # don't worry I'll explain this logic
return result
def Ceaser_decrypt(text, s):
result = "" # this is for storing end decrypted
# Let us visit our input (text) alpha by alpha :)
for i in range(len(text)):
char = text[i]
result += chr((ord(char) - s +26- 97) % 26 + 97) # this is what we are interested in
# In encryption we added the shift 's' here we will be doin the reverse :)
# Why I've added 26?
# Because we have modulo 26 and it is wrap around..
return result
if __name__=="__main__":
Key=int(input("Enter Key/Shift: "))
# Plain=input()
# BUt hold on, we are gonna consider only lowercase alphabets as text into our 'Ceaser_encrypt'.
# let us lowercase 'Plain' and split it over spaces into list and join back.
# in other words let us remove all spaces from 'Plain' and lowercase everything
Plain=''.join(input("PlainText : ").lower().split())
CipherText=Ceaser_encrypt(Plain,Key)
print("CipherText: ",CipherText)
# let us now try to decrypt it :)
print("DecipheredText: ",Ceaser_decrypt(CipherText,Key))
# It is working.. LEt us discuss this logic
@cypher-nullbyte
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment