Created
January 15, 2021 17:21
-
-
Save anmolj7/9864798efa749157b66c4d49fa609c90 to your computer and use it in GitHub Desktop.
Just a small code to encrypt and decrypt caesar cypher.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' | |
def encrypt(string, shift=1): | |
''' | |
Caesar Cypher shifts the letters by the given shift, for example, if key=1 | |
then, a becomes b, be becomes c, and c becomes d, etc | |
''' | |
assert 1<=shift<26 | |
words = string.split() #Splitting by spaces | |
words_list = [] | |
for word in words: | |
current_word = '' | |
for letter in word: | |
letter = letter.upper() | |
index = LETTERS.index(letter)+shift | |
current_word += LETTERS[index%26] | |
words_list.append(current_word) | |
return ' '.join(words_list) | |
def decrypt(string, shift): | |
words = [] | |
for word in string.split(): | |
print(f'Current word: {word}') | |
current_word = '' | |
for letter in word: | |
index = LETTERS.index(letter) | |
current_word += LETTERS[index-shift] | |
words.append(current_word) | |
return ' '.join(words) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment