-
-
Save belminf/c915e8615e593a46f0d4ad4e35063048 to your computer and use it in GitHub Desktop.
An implementation of a simple caesar cipher
This file contains hidden or 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
# This is an attempt at creating a caesar cipher using the ord() | |
# function and taking input from the user. | |
import re | |
def caesar_cipher(mode, input_str, key): | |
return_str = '' | |
char_as_num = None | |
num_as_char = None | |
char_after_cipher = None | |
if mode == 'ENCRYPT': | |
for char in input_str: | |
char_as_num = ord(char) | |
char_after_cipher = char_as_num + key | |
if re.match(r"[^A-Za-z]", char): | |
return_str += char | |
# Why not just "char == char.upper()"? --BF | |
elif char == char.lower().upper(): | |
if char_after_cipher > 90: | |
num_as_char = chr(char_after_cipher - 26) | |
return_str += num_as_char | |
else: | |
num_as_char = chr(char_after_cipher) | |
return_str += num_as_char | |
else: | |
if char_after_cipher > 122: | |
num_as_char = chr(char_after_cipher - 26) | |
return_str += num_as_char | |
else: | |
num_as_char = chr(char_after_cipher) | |
return_str += num_as_char | |
if mode == 'DECRYPT': | |
for char in input_str: | |
char_as_num = ord(char) | |
char_after_cipher = char_as_num - key | |
if re.match(r"[^A-Za-z]", char): | |
return_str += char | |
# Same as L19 | |
elif char == char.lower().upper(): | |
if char_after_cipher < 65: | |
num_as_char = chr(char_after_cipher + 26) | |
return_str += num_as_char | |
else: | |
num_as_char = chr(char_after_cipher) | |
return_str += num_as_char | |
else: | |
if char_after_cipher < 97: | |
num_as_char = chr(char_after_cipher + 26) | |
return_str += num_as_char | |
else: | |
num_as_char = chr(char_after_cipher) | |
return_str += num_as_char | |
return return_str | |
def validate_entry(variable, input_str): | |
return_input = input_str | |
while True: | |
# Instead of using regex, this would be less costly and more readable: | |
# if variable == 'mode' and return_input.upper() in ('ENCRYPT', 'DECRPYPT'): | |
if variable == 'mode' and re.match(r"(^ENCRYPT|^DECRYPT)$", \ | |
return_input.upper()) == None: | |
print('"{0}" is not a valid input for mode...'.format(return_input)) | |
return_input = input('> ') | |
# Same as L64 | |
elif variable == 'key' and re.match(r"(^[1-9]|^1[0-9]|^2[0-5])$", \ | |
return_input) == None: | |
print('"{0}" is not a valid input for key...'.format(return_input)) | |
return_input = input('> ') | |
else: | |
break | |
return return_input | |
def main(): | |
mode = None | |
input_str = None | |
result = None | |
key = None | |
print('Welcome to a simple caesar cipher!') | |
mode = input('Choose mode (encrypt/decrypt): ') | |
mode = validate_entry('mode', mode) | |
print('') | |
print('Enter the string that you want to {0}:'.format(mode.lower())) | |
input_str = input() | |
print('') | |
key = input('Choose key (1-25): ') | |
key = validate_entry('key', key) | |
print('') | |
result = caesar_cipher(mode.upper(), input_str, int(key)) | |
print('Result: {0}'.format(result)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment