From 6312aac2737c47e5fda6af43e1092eccca4f2fb1 Mon Sep 17 00:00:00 2001 From: Satria H R Harsono Date: Tue, 6 Feb 2018 18:51:20 +0700 Subject: [PATCH 1/2] Initial code --- simple_substitution_cipher.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 simple_substitution_cipher.py diff --git a/simple_substitution_cipher.py b/simple_substitution_cipher.py new file mode 100644 index 0000000..f2e825b --- /dev/null +++ b/simple_substitution_cipher.py @@ -0,0 +1,30 @@ +def input_key(key_dict): + # expecting string representation of dictionary + # ie {'a':'b', 'b':'c', . . .} + import ast + + # for now, we expect everything fit in, lol + return ast.literal_eval(key_dict) + + +def encrypt(plaintext, key): + # expecting plaintext of string + # expecting key from input_key + ciphertext = "" + + for p in plaintext: + ciphertext += key[p] + + # again, we expect no error + return ciphertext + + +def decrypt(message, key): + # duplicated from encrypt :v + # please be aware that the key used in decrypt is way in encrypt + plaintext = "" + + for c in ciphertext: + plaintext += key[c] + + return plaintext From d834085a19c7d54105bd4b14f39edfe1a4aa788d Mon Sep 17 00:00:00 2001 From: Satria H R Harsono Date: Wed, 7 Feb 2018 10:35:12 +0700 Subject: [PATCH 2/2] Fix error --- simple_substitution_cipher.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/simple_substitution_cipher.py b/simple_substitution_cipher.py index f2e825b..5a9d34e 100644 --- a/simple_substitution_cipher.py +++ b/simple_substitution_cipher.py @@ -13,18 +13,30 @@ def encrypt(plaintext, key): ciphertext = "" for p in plaintext: - ciphertext += key[p] + if p in key: + ciphertext += key[p] + else: + if p == ' ': + ciphertext += c + else: + ciphertext += '-' # again, we expect no error return ciphertext -def decrypt(message, key): +def decrypt(ciphertext, key): # duplicated from encrypt :v # please be aware that the key used in decrypt is way in encrypt plaintext = "" - + for c in ciphertext: - plaintext += key[c] + if c in key: + plaintext += key[c] + else: + if c == ' ': + plaintext += c + else: + plaintext += '-' return plaintext