From d62771a42a0271af09f7288a920a0e1d464cd45f Mon Sep 17 00:00:00 2001 From: yuriimchg Date: Sat, 12 Oct 2019 13:46:46 +0300 Subject: [PATCH 1/7] change var names for better reading --- ciphers/caesar_cipher.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/ciphers/caesar_cipher.py b/ciphers/caesar_cipher.py index 3f24e049afb0..98cb08922183 100644 --- a/ciphers/caesar_cipher.py +++ b/ciphers/caesar_cipher.py @@ -1,6 +1,6 @@ -def encrypt(strng, key): +def encrypt(input_string, key): encrypted = "" - for x in strng: + for x in input_string: indx = (ord(x) + key) % 256 if indx > 126: indx = indx - 95 @@ -8,9 +8,9 @@ def encrypt(strng, key): return encrypted -def decrypt(strng, key): +def decrypt(input_string, key): decrypted = "" - for x in strng: + for x in input_string: indx = (ord(x) - key) % 256 if indx < 32: indx = indx + 95 @@ -18,11 +18,11 @@ def decrypt(strng, key): return decrypted -def brute_force(strng): +def brute_force(input_string): key = 1 decrypted = "" while key <= 94: - for x in strng: + for x in input_string: indx = (ord(x) - key) % 256 if indx < 32: indx = indx + 95 @@ -44,18 +44,18 @@ def main(): if choice not in ["1", "2", "3", "4"]: print("Invalid choice, please enter a valid choice") elif choice == "1": - strng = input("Please enter the string to be encrypted: ") + input_string = input("Please enter the string to be encrypted: ") key = int(input("Please enter off-set between 1-94: ")) if key in range(1, 95): - print(encrypt(strng.lower(), key)) + print(encrypt(input_string.lower(), key)) elif choice == "2": - strng = input("Please enter the string to be decrypted: ") + input_string = input("Please enter the string to be decrypted: ") key = int(input("Please enter off-set between 1-94: ")) if key in range(1, 95): - print(decrypt(strng, key)) + print(decrypt(input_string, key)) elif choice == "3": - strng = input("Please enter the string to be decrypted: ") - brute_force(strng) + input_string = input("Please enter the string to be decrypted: ") + brute_force(input_string) main() elif choice == "4": print("Goodbye.") From ecd822fa4b4b38adc5f8baf5c87a840750251e89 Mon Sep 17 00:00:00 2001 From: yuriimchg Date: Sat, 12 Oct 2019 14:38:08 +0300 Subject: [PATCH 2/7] rewrite encrypt function to fix ascii char ranges --- ciphers/caesar_cipher.py | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/ciphers/caesar_cipher.py b/ciphers/caesar_cipher.py index 98cb08922183..6809b0e7018d 100644 --- a/ciphers/caesar_cipher.py +++ b/ciphers/caesar_cipher.py @@ -1,34 +1,36 @@ def encrypt(input_string, key): - encrypted = "" + result = "" for x in input_string: - indx = (ord(x) + key) % 256 - if indx > 126: - indx = indx - 95 - encrypted = encrypted + chr(indx) - return encrypted + if not x.isalpha(): + result += x + elif x.isupper(): + result += chr((ord(x) + key - 65) % 26 + 65) + elif x.islower(): + result += chr((ord(x) + key - 97) % 26 + 97) + return result def decrypt(input_string, key): - decrypted = "" + result = "" for x in input_string: indx = (ord(x) - key) % 256 - if indx < 32: - indx = indx + 95 - decrypted = decrypted + chr(indx) - return decrypted + if indx < 65: + indx = indx + 26 + result = result + chr(indx) + return result def brute_force(input_string): key = 1 - decrypted = "" + result = "" while key <= 94: for x in input_string: indx = (ord(x) - key) % 256 if indx < 32: indx = indx + 95 - decrypted = decrypted + chr(indx) - print("Key: {}\t| Message: {}".format(key, decrypted)) - decrypted = "" + result = result + chr(indx) + print("Key: {}\t| Message: {}".format(key, result)) + result = "" key += 1 return None @@ -45,7 +47,7 @@ def main(): print("Invalid choice, please enter a valid choice") elif choice == "1": input_string = input("Please enter the string to be encrypted: ") - key = int(input("Please enter off-set between 1-94: ")) + key = int(input("Please enter off-set between 0-25: ")) if key in range(1, 95): print(encrypt(input_string.lower(), key)) elif choice == "2": From 6b8d5c062e075baade175cfdd23fa44f851eabb2 Mon Sep 17 00:00:00 2001 From: yuriimchg Date: Sat, 12 Oct 2019 14:42:08 +0300 Subject: [PATCH 3/7] fix decrypt function --- ciphers/caesar_cipher.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/ciphers/caesar_cipher.py b/ciphers/caesar_cipher.py index 6809b0e7018d..602aa6df3f42 100644 --- a/ciphers/caesar_cipher.py +++ b/ciphers/caesar_cipher.py @@ -13,10 +13,12 @@ def encrypt(input_string, key): def decrypt(input_string, key): result = "" for x in input_string: - indx = (ord(x) - key) % 256 - if indx < 65: - indx = indx + 26 - result = result + chr(indx) + if not x.isalpha(): + result += x + elif x.isupper(): + result += chr((ord(x) - key - 65) % 26 + 65) + elif x.islower(): + result += chr((ord(x) - key - 97) % 26 + 97) return result From daa5587a4ad762c222bdc3ec63d1db16dd03c99f Mon Sep 17 00:00:00 2001 From: yuriimchg Date: Sat, 12 Oct 2019 14:47:45 +0300 Subject: [PATCH 4/7] update formatting (add f-strings) --- ciphers/caesar_cipher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ciphers/caesar_cipher.py b/ciphers/caesar_cipher.py index 602aa6df3f42..a351e9dfcce5 100644 --- a/ciphers/caesar_cipher.py +++ b/ciphers/caesar_cipher.py @@ -31,7 +31,7 @@ def brute_force(input_string): if indx < 32: indx = indx + 95 result = result + chr(indx) - print("Key: {}\t| Message: {}".format(key, result)) + print(f"Key: {key}\t| Message: {result}") result = "" key += 1 return None From 794aa7a45054a9b7a038297a647b16c0b45b2112 Mon Sep 17 00:00:00 2001 From: yuriimchg Date: Sat, 12 Oct 2019 14:47:45 +0300 Subject: [PATCH 5/7] upd fuctions --- ciphers/caesar_cipher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ciphers/caesar_cipher.py b/ciphers/caesar_cipher.py index 602aa6df3f42..a351e9dfcce5 100644 --- a/ciphers/caesar_cipher.py +++ b/ciphers/caesar_cipher.py @@ -31,7 +31,7 @@ def brute_force(input_string): if indx < 32: indx = indx + 95 result = result + chr(indx) - print("Key: {}\t| Message: {}".format(key, result)) + print(f"Key: {key}\t| Message: {result}") result = "" key += 1 return None From f313ded721d5864090e40816ab3be115460bf7ce Mon Sep 17 00:00:00 2001 From: yuriimchg Date: Sat, 12 Oct 2019 14:53:00 +0300 Subject: [PATCH 6/7] add f-string formatting (python3.6+) --- ciphers/caesar_cipher.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/ciphers/caesar_cipher.py b/ciphers/caesar_cipher.py index a351e9dfcce5..61212810f631 100644 --- a/ciphers/caesar_cipher.py +++ b/ciphers/caesar_cipher.py @@ -1,5 +1,5 @@ def encrypt(input_string, key): - result = "" + result = '' for x in input_string: if not x.isalpha(): result += x @@ -11,7 +11,7 @@ def encrypt(input_string, key): def decrypt(input_string, key): - result = "" + result = '' for x in input_string: if not x.isalpha(): result += x @@ -24,26 +24,23 @@ def decrypt(input_string, key): def brute_force(input_string): key = 1 - result = "" + result = '' while key <= 94: for x in input_string: indx = (ord(x) - key) % 256 if indx < 32: indx = indx + 95 result = result + chr(indx) - print(f"Key: {key}\t| Message: {result}") - result = "" + print(f'Key: {key}\t| Message: {result}') + result = '' key += 1 return None def main(): while True: - print("-" * 10 + "\n**Menu**\n" + "-" * 10) - print("1.Encrpyt") - print("2.Decrypt") - print("3.BruteForce") - print("4.Quit") + print(f'{"-" * 10}\n Menu\n{"-", * 10}') + print(*["1.Encrpyt", "2.Decrypt", "3.BruteForce", "4.Quit"], sep='\n') choice = input("What would you like to do?: ") if choice not in ["1", "2", "3", "4"]: print("Invalid choice, please enter a valid choice") From 187517cedf8472afdcd15a4d20cdeda905b73503 Mon Sep 17 00:00:00 2001 From: yuriimchg Date: Sat, 12 Oct 2019 14:57:11 +0300 Subject: [PATCH 7/7] add type hints (python3.4+) --- ciphers/caesar_cipher.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/ciphers/caesar_cipher.py b/ciphers/caesar_cipher.py index 1a12691503bc..52155bbdc49e 100644 --- a/ciphers/caesar_cipher.py +++ b/ciphers/caesar_cipher.py @@ -1,4 +1,4 @@ -def encrypt(input_string, key): +def encrypt(input_string: str, key: int) -> str: result = '' for x in input_string: if not x.isalpha(): @@ -10,7 +10,7 @@ def encrypt(input_string, key): return result -def decrypt(input_string, key): +def decrypt(input_string: str, key: int) -> str: result = '' for x in input_string: if not x.isalpha(): @@ -22,7 +22,7 @@ def decrypt(input_string, key): return result -def brute_force(input_string): +def brute_force(input_string: str) -> None: key = 1 result = '' while key <= 94: @@ -33,7 +33,6 @@ def brute_force(input_string): result = result + chr(indx) print(f'Key: {key}\t| Message: {result}') result = '' - key += 1 return None