Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create caeser_cipher.py #182

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions DSA/Cryptography/caeser_cipher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# import string module to use ascii_lowercase
import string

# define alphabet variable to use ascii_lowercase
alphabet = string.ascii_lowercase

# define encryption function
def encrypt(text, shift):
# define empty string to store encrypted text
encrypted_text = ""
# loop through each character in text
for char in text:
# if character is not in alphabet, add to encrypted_text
if char not in alphabet:
encrypted_text += char
# else, add shifted character to encrypted_text
else:
encrypted_text += alphabet[(alphabet.index(char) + shift) % 26]
# return encrypted_text
return encrypted_text

# define decryption function
def decrypt(text, shift):
# define empty string to store decrypted text
decrypted_text = ""
# loop through each character in text
for char in text:
# if character is not in alphabet, add to decrypted_text
if char not in alphabet:
decrypted_text += char
# else, add shifted character to decrypted_text
else:
decrypted_text += alphabet[(alphabet.index(char) - shift) % 26]
# return decrypted_text
return decrypted_text

# define main function
def main():
# define text variable
plain_text = input("Enter Some Text: ")
# define shift variable
key = int(input("Enter Key: "))
# call encrypt function with text and shift variables
encrypted_text = encrypt(plain_text, key)
# print encrypted_text
print(encrypted_text)
# call decrypt function with encrypted_text and shift variables
decrypted_text = decrypt(encrypted_text, key)
# print decrypted_text
print(decrypted_text)

# call main function
main()