2021-csci-ga3205-midterm/index.py

42 lines
1.2 KiB
Python
Raw Normal View History

2021-10-24 17:52:23 +00:00
import argparse
def caesar(p_text_letter, key_letter):
p_text_num = ord(p_text_letter) - ord('A')
key_letter_num = ord(key_letter) - ord('A')
return chr((p_text_num + key_letter_num) % 26 + ord('A'))
2021-10-24 17:52:23 +00:00
def caesar_reverse(c_text_letter, key_letter):
c_text_num = ord(c_text_letter) - ord('A')
key_letter_num = ord(key_letter) - ord('A')
return chr((c_text_num - key_letter_num) % 26 + ord('A'))
2021-10-24 18:26:19 +00:00
2021-10-24 17:52:23 +00:00
def encryption(plaintext, key):
ciphertext = ''
for i in range(len(plaintext)):
ciphertext += caesar(plaintext[i], key[i % len(key)])
return ciphertext
def decryption(ciphertext, key):
2021-10-24 18:26:19 +00:00
plaintext = ''
for i in range(len(ciphertext)):
plaintext += caesar_reverse(ciphertext[i], key[i % len(key)])
return plaintext.lower()
2021-10-24 17:52:23 +00:00
def main():
parser = argparse.ArgumentParser()
2021-10-24 18:26:19 +00:00
parser.add_argument('--text', type=str)
2021-10-24 17:52:23 +00:00
parser.add_argument('--key', type=str)
args = parser.parse_args()
print("Encrypted ciphertext: " + encryption(args.text.upper().replace(" ", ""), args.key))
print("Decrypted plaintext: " + decryption(args.text.lower().replace(" ", ""), args.key))
2021-10-24 17:52:23 +00:00
if __name__ == '__main__':
main()