From e5519b99d3165b29cbaf4870ba769f751f21397d Mon Sep 17 00:00:00 2001 From: RadadiyaMohit <30775542+radadiyamohit81@users.noreply.github.com> Date: Sun, 11 Oct 2020 22:49:14 +0530 Subject: [PATCH 01/11] create beaufort cipher if you like my code, merge it and add the label as `hacktoberfest-accepted` --- ciphers/beaufort_cipher.py | 72 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 ciphers/beaufort_cipher.py diff --git a/ciphers/beaufort_cipher.py b/ciphers/beaufort_cipher.py new file mode 100644 index 000000000000..90163af98507 --- /dev/null +++ b/ciphers/beaufort_cipher.py @@ -0,0 +1,72 @@ +dict1 = {'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, + 'F': 5, 'G': 6, 'H': 7, 'I': 8, 'J': 9, + 'K': 10, 'L': 11, 'M': 12, 'N': 13, 'O': 14, + 'P': 15, 'Q': 16, 'R': 17, 'S': 18, 'T': 19, + 'U': 20, 'V': 21, 'W': 22, 'X': 23, 'Y': 24, 'Z': 25} + +dict2 = {0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E', + 5: 'F', 6: 'G', 7: 'H', 8: 'I', 9: 'J', + 10: 'K', 11: 'L', 12: 'M', 13: 'N', 14: 'O', + 15: 'P', 16: 'Q', 17: 'R', 18: 'S', 19: 'T', + 20: 'U', 21: 'V', 22: 'W', 23: 'X', 24: 'Y', 25: 'Z'} + + +# This function generates the key in +# a cyclic manner until it's length isn't +# equal to the length of original text +def generate_key(message, key): + x = len(message) + i = 0 + while True: + if x == i: + i = 0 + if len(key) == len(message): + break + key += key[i] + i += 1 + return key + + +# This function returns the encrypted text +# generated with the help of the key +def cipherText(message, key_new): + cipher_text = '' + i = 0 + for letter in message: + if letter == ' ': + cipher_text += ' ' + else: + x = (dict1[letter]-dict1[key_new[i]]) % 26 + i += 1 + cipher_text += dict2[x] + return cipher_text + + +# This function decrypts the encrypted text +# and returns the original text +def originalText(cipher_text, key_new): + or_txt = '' + i = 0 + for letter in cipher_text: + if letter == ' ': + or_txt += ' ' + else: + x = (dict1[letter]+dict1[key_new[i]]+26) % 26 + i += 1 + or_txt += dict2[x] + return or_txt + + +def main(): + message = 'THE GERMAN ATTACK' + key = 'SECRET' + key_new = generate_key(message, key) + cipher_text = cipherText(message, key_new) + original_text = originalText(cipher_text, key_new) + print("Encrypted Text =", cipher_text) + print("Original Text =", original_text) + + +# Executes the main function +if __name__ == '__main__': + main() From 153b45a025eda74f01901fbb46d66986271aabe0 Mon Sep 17 00:00:00 2001 From: RadadiyaMohit <30775542+radadiyamohit81@users.noreply.github.com> Date: Sat, 17 Oct 2020 13:31:07 +0530 Subject: [PATCH 02/11] update the file --- ciphers/beaufort_cipher.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ciphers/beaufort_cipher.py b/ciphers/beaufort_cipher.py index 90163af98507..ae23ceb665dc 100644 --- a/ciphers/beaufort_cipher.py +++ b/ciphers/beaufort_cipher.py @@ -14,7 +14,7 @@ # This function generates the key in # a cyclic manner until it's length isn't # equal to the length of original text -def generate_key(message, key): +def generate_key(message: str, key: str) -> str: x = len(message) i = 0 while True: @@ -29,7 +29,7 @@ def generate_key(message, key): # This function returns the encrypted text # generated with the help of the key -def cipherText(message, key_new): +def cipherText(message: str, key_new: str) -> str: cipher_text = '' i = 0 for letter in message: @@ -44,7 +44,7 @@ def cipherText(message, key_new): # This function decrypts the encrypted text # and returns the original text -def originalText(cipher_text, key_new): +def originalText(cipher_text: str, key_new: str) -> str: or_txt = '' i = 0 for letter in cipher_text: From 53d1d2cccbe63d3be65e928f62980092b9bc479d Mon Sep 17 00:00:00 2001 From: RadadiyaMohit <30775542+radadiyamohit81@users.noreply.github.com> Date: Sat, 17 Oct 2020 13:37:57 +0530 Subject: [PATCH 03/11] Update beaufort_cipher.py --- ciphers/beaufort_cipher.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/ciphers/beaufort_cipher.py b/ciphers/beaufort_cipher.py index ae23ceb665dc..2eaa01e2398c 100644 --- a/ciphers/beaufort_cipher.py +++ b/ciphers/beaufort_cipher.py @@ -66,7 +66,4 @@ def main(): print("Encrypted Text =", cipher_text) print("Original Text =", original_text) - -# Executes the main function -if __name__ == '__main__': - main() +main() From c8b66ed28059ac3084eedf906fd16fbe4c4dd7b8 Mon Sep 17 00:00:00 2001 From: RadadiyaMohit <30775542+radadiyamohit81@users.noreply.github.com> Date: Sat, 17 Oct 2020 13:38:26 +0530 Subject: [PATCH 04/11] Update beaufort_cipher.py From 696707bcb08f8fb9393f60be05680d85fba3c7ab Mon Sep 17 00:00:00 2001 From: RadadiyaMohit <30775542+radadiyamohit81@users.noreply.github.com> Date: Sat, 17 Oct 2020 14:24:16 +0530 Subject: [PATCH 05/11] update as per black formatter --- ciphers/beaufort_cipher.py | 90 +++++++++++++++++++++++++++++--------- 1 file changed, 69 insertions(+), 21 deletions(-) diff --git a/ciphers/beaufort_cipher.py b/ciphers/beaufort_cipher.py index 2eaa01e2398c..7b6037358f64 100644 --- a/ciphers/beaufort_cipher.py +++ b/ciphers/beaufort_cipher.py @@ -1,14 +1,60 @@ -dict1 = {'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, - 'F': 5, 'G': 6, 'H': 7, 'I': 8, 'J': 9, - 'K': 10, 'L': 11, 'M': 12, 'N': 13, 'O': 14, - 'P': 15, 'Q': 16, 'R': 17, 'S': 18, 'T': 19, - 'U': 20, 'V': 21, 'W': 22, 'X': 23, 'Y': 24, 'Z': 25} +dict1 = { + "A": 0, + "B": 1, + "C": 2, + "D": 3, + "E": 4, + "F": 5, + "G": 6, + "H": 7, + "I": 8, + "J": 9, + "K": 10, + "L": 11, + "M": 12, + "N": 13, + "O": 14, + "P": 15, + "Q": 16, + "R": 17, + "S": 18, + "T": 19, + "U": 20, + "V": 21, + "W": 22, + "X": 23, + "Y": 24, + "Z": 25, +} -dict2 = {0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E', - 5: 'F', 6: 'G', 7: 'H', 8: 'I', 9: 'J', - 10: 'K', 11: 'L', 12: 'M', 13: 'N', 14: 'O', - 15: 'P', 16: 'Q', 17: 'R', 18: 'S', 19: 'T', - 20: 'U', 21: 'V', 22: 'W', 23: 'X', 24: 'Y', 25: 'Z'} +dict2 = { + 0: "A", + 1: "B", + 2: "C", + 3: "D", + 4: "E", + 5: "F", + 6: "G", + 7: "H", + 8: "I", + 9: "J", + 10: "K", + 11: "L", + 12: "M", + 13: "N", + 14: "O", + 15: "P", + 16: "Q", + 17: "R", + 18: "S", + 19: "T", + 20: "U", + 21: "V", + 22: "W", + 23: "X", + 24: "Y", + 25: "Z", +} # This function generates the key in @@ -30,13 +76,13 @@ def generate_key(message: str, key: str) -> str: # This function returns the encrypted text # generated with the help of the key def cipherText(message: str, key_new: str) -> str: - cipher_text = '' + cipher_text = "" i = 0 for letter in message: - if letter == ' ': - cipher_text += ' ' + if letter == " ": + cipher_text += " " else: - x = (dict1[letter]-dict1[key_new[i]]) % 26 + x = (dict1[letter] - dict1[key_new[i]]) % 26 i += 1 cipher_text += dict2[x] return cipher_text @@ -45,25 +91,27 @@ def cipherText(message: str, key_new: str) -> str: # This function decrypts the encrypted text # and returns the original text def originalText(cipher_text: str, key_new: str) -> str: - or_txt = '' + or_txt = "" i = 0 for letter in cipher_text: - if letter == ' ': - or_txt += ' ' + if letter == " ": + or_txt += " " else: - x = (dict1[letter]+dict1[key_new[i]]+26) % 26 + x = (dict1[letter] + dict1[key_new[i]] + 26) % 26 i += 1 or_txt += dict2[x] return or_txt def main(): - message = 'THE GERMAN ATTACK' - key = 'SECRET' + message = "THE GERMAN ATTACK" + key = "SECRET" key_new = generate_key(message, key) cipher_text = cipherText(message, key_new) original_text = originalText(cipher_text, key_new) print("Encrypted Text =", cipher_text) print("Original Text =", original_text) -main() + +if __name__ == "__main__": + main() From 626fa2932ad6236c587cd2750ab700c49ec3f8dc Mon Sep 17 00:00:00 2001 From: RadadiyaMohit <30775542+radadiyamohit81@users.noreply.github.com> Date: Sat, 17 Oct 2020 14:42:42 +0530 Subject: [PATCH 06/11] Update beaufort_cipher.py --- ciphers/beaufort_cipher.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ciphers/beaufort_cipher.py b/ciphers/beaufort_cipher.py index 7b6037358f64..d1dae80e5816 100644 --- a/ciphers/beaufort_cipher.py +++ b/ciphers/beaufort_cipher.py @@ -61,6 +61,9 @@ # a cyclic manner until it's length isn't # equal to the length of original text def generate_key(message: str, key: str) -> str: + """ + Return the generated key + """ x = len(message) i = 0 while True: @@ -76,6 +79,9 @@ def generate_key(message: str, key: str) -> str: # This function returns the encrypted text # generated with the help of the key def cipherText(message: str, key_new: str) -> str: + """ + Return the CipherText + """ cipher_text = "" i = 0 for letter in message: @@ -91,6 +97,9 @@ def cipherText(message: str, key_new: str) -> str: # This function decrypts the encrypted text # and returns the original text def originalText(cipher_text: str, key_new: str) -> str: + """ + Return the original text + """ or_txt = "" i = 0 for letter in cipher_text: From 5e9a03d3cc3c7661650a39b5d2b2b8357886b78b Mon Sep 17 00:00:00 2001 From: RadadiyaMohit <30775542+radadiyamohit81@users.noreply.github.com> Date: Sat, 17 Oct 2020 14:55:00 +0530 Subject: [PATCH 07/11] update the file --- ciphers/beaufort_cipher.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/ciphers/beaufort_cipher.py b/ciphers/beaufort_cipher.py index d1dae80e5816..3040922c8ea3 100644 --- a/ciphers/beaufort_cipher.py +++ b/ciphers/beaufort_cipher.py @@ -1,3 +1,7 @@ +""" +Author: Mohit Radadiya +""" + dict1 = { "A": 0, "B": 1, @@ -62,7 +66,8 @@ # equal to the length of original text def generate_key(message: str, key: str) -> str: """ - Return the generated key + >>> generate_key("THE GERMAN ATTACK","SECRET") + SECRETSECRETSECRE """ x = len(message) i = 0 @@ -80,7 +85,8 @@ def generate_key(message: str, key: str) -> str: # generated with the help of the key def cipherText(message: str, key_new: str) -> str: """ - Return the CipherText + >>> cipherText("THE GERMAN ATTACK","SECRETSECRETSECRE") + BDC PAYUWL JPAIYI """ cipher_text = "" i = 0 @@ -98,7 +104,8 @@ def cipherText(message: str, key_new: str) -> str: # and returns the original text def originalText(cipher_text: str, key_new: str) -> str: """ - Return the original text + >>> originalText("BDC PAYUWL JPAIYI","SECRETSECRETSECRE") + THE GERMAN ATTACK """ or_txt = "" i = 0 @@ -123,4 +130,7 @@ def main(): if __name__ == "__main__": + import doctest + + doctest.testmod() main() From ece028822cb1cf409bef3d2a035822752b1d7002 Mon Sep 17 00:00:00 2001 From: RadadiyaMohit <30775542+radadiyamohit81@users.noreply.github.com> Date: Sat, 17 Oct 2020 17:51:27 +0530 Subject: [PATCH 08/11] update file --- ciphers/beaufort_cipher.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ciphers/beaufort_cipher.py b/ciphers/beaufort_cipher.py index 3040922c8ea3..5538c2822c4d 100644 --- a/ciphers/beaufort_cipher.py +++ b/ciphers/beaufort_cipher.py @@ -66,7 +66,7 @@ # equal to the length of original text def generate_key(message: str, key: str) -> str: """ - >>> generate_key("THE GERMAN ATTACK","SECRET") + >>> generate_key('THE GERMAN ATTACK','SECRET') SECRETSECRETSECRE """ x = len(message) @@ -85,7 +85,7 @@ def generate_key(message: str, key: str) -> str: # generated with the help of the key def cipherText(message: str, key_new: str) -> str: """ - >>> cipherText("THE GERMAN ATTACK","SECRETSECRETSECRE") + >>> cipherText('THE GERMAN ATTACK','SECRETSECRETSECRE') BDC PAYUWL JPAIYI """ cipher_text = "" @@ -104,7 +104,7 @@ def cipherText(message: str, key_new: str) -> str: # and returns the original text def originalText(cipher_text: str, key_new: str) -> str: """ - >>> originalText("BDC PAYUWL JPAIYI","SECRETSECRETSECRE") + >>> originalText('BDC PAYUWL JPAIYI','SECRETSECRETSECRE') THE GERMAN ATTACK """ or_txt = "" From 2fe510362c1b0b0742aa61c9960d1f2e6d078caa Mon Sep 17 00:00:00 2001 From: RadadiyaMohit <30775542+radadiyamohit81@users.noreply.github.com> Date: Sat, 17 Oct 2020 17:58:54 +0530 Subject: [PATCH 09/11] update file --- ciphers/beaufort_cipher.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ciphers/beaufort_cipher.py b/ciphers/beaufort_cipher.py index 5538c2822c4d..b0b21353d953 100644 --- a/ciphers/beaufort_cipher.py +++ b/ciphers/beaufort_cipher.py @@ -66,8 +66,8 @@ # equal to the length of original text def generate_key(message: str, key: str) -> str: """ - >>> generate_key('THE GERMAN ATTACK','SECRET') - SECRETSECRETSECRE + >>> generate_key("THE GERMAN ATTACK","SECRET") + 'SECRETSECRETSECRE' """ x = len(message) i = 0 @@ -85,8 +85,8 @@ def generate_key(message: str, key: str) -> str: # generated with the help of the key def cipherText(message: str, key_new: str) -> str: """ - >>> cipherText('THE GERMAN ATTACK','SECRETSECRETSECRE') - BDC PAYUWL JPAIYI + >>> cipherText("THE GERMAN ATTACK","SECRETSECRETSECRE") + 'BDC PAYUWL JPAIYI' """ cipher_text = "" i = 0 @@ -104,8 +104,8 @@ def cipherText(message: str, key_new: str) -> str: # and returns the original text def originalText(cipher_text: str, key_new: str) -> str: """ - >>> originalText('BDC PAYUWL JPAIYI','SECRETSECRETSECRE') - THE GERMAN ATTACK + >>> originalText("BDC PAYUWL JPAIYI","SECRETSECRETSECRE") + 'THE GERMAN ATTACK' """ or_txt = "" i = 0 From e13c006d2ddc7e6c5df293133ee0f3fd71e8b46d Mon Sep 17 00:00:00 2001 From: RadadiyaMohit <30775542+radadiyamohit81@users.noreply.github.com> Date: Sat, 17 Oct 2020 18:57:06 +0530 Subject: [PATCH 10/11] update file --- ciphers/beaufort_cipher.py | 72 +++++--------------------------------- 1 file changed, 9 insertions(+), 63 deletions(-) diff --git a/ciphers/beaufort_cipher.py b/ciphers/beaufort_cipher.py index b0b21353d953..3d42da90d79a 100644 --- a/ciphers/beaufort_cipher.py +++ b/ciphers/beaufort_cipher.py @@ -2,63 +2,10 @@ Author: Mohit Radadiya """ -dict1 = { - "A": 0, - "B": 1, - "C": 2, - "D": 3, - "E": 4, - "F": 5, - "G": 6, - "H": 7, - "I": 8, - "J": 9, - "K": 10, - "L": 11, - "M": 12, - "N": 13, - "O": 14, - "P": 15, - "Q": 16, - "R": 17, - "S": 18, - "T": 19, - "U": 20, - "V": 21, - "W": 22, - "X": 23, - "Y": 24, - "Z": 25, -} +from string import ascii_uppercase -dict2 = { - 0: "A", - 1: "B", - 2: "C", - 3: "D", - 4: "E", - 5: "F", - 6: "G", - 7: "H", - 8: "I", - 9: "J", - 10: "K", - 11: "L", - 12: "M", - 13: "N", - 14: "O", - 15: "P", - 16: "Q", - 17: "R", - 18: "S", - 19: "T", - 20: "U", - 21: "V", - 22: "W", - 23: "X", - 24: "Y", - 25: "Z", -} +dict1 = {char: i for i, char in enumerate(ascii_uppercase)} +dict2 = {i: char for i, char in enumerate(ascii_uppercase)} # This function generates the key in @@ -83,9 +30,9 @@ def generate_key(message: str, key: str) -> str: # This function returns the encrypted text # generated with the help of the key -def cipherText(message: str, key_new: str) -> str: +def cipher_text(message: str, key_new: str) -> str: """ - >>> cipherText("THE GERMAN ATTACK","SECRETSECRETSECRE") + >>> cipher_text("THE GERMAN ATTACK","SECRETSECRETSECRE") 'BDC PAYUWL JPAIYI' """ cipher_text = "" @@ -102,7 +49,7 @@ def cipherText(message: str, key_new: str) -> str: # This function decrypts the encrypted text # and returns the original text -def originalText(cipher_text: str, key_new: str) -> str: +def original_text(cipher_text: str, key_new: str) -> str: """ >>> originalText("BDC PAYUWL JPAIYI","SECRETSECRETSECRE") 'THE GERMAN ATTACK' @@ -123,10 +70,9 @@ def main(): message = "THE GERMAN ATTACK" key = "SECRET" key_new = generate_key(message, key) - cipher_text = cipherText(message, key_new) - original_text = originalText(cipher_text, key_new) - print("Encrypted Text =", cipher_text) - print("Original Text =", original_text) + s = cipher_text(message, key_new) + print(f"Encrypted Text = {s}") + print(f"Original Text = {original_text(s, key_new)}") if __name__ == "__main__": From f23e0e282dbf44a2ef7ff3a27c8a25e6a5b9cda8 Mon Sep 17 00:00:00 2001 From: RadadiyaMohit <30775542+radadiyamohit81@users.noreply.github.com> Date: Sat, 17 Oct 2020 19:11:56 +0530 Subject: [PATCH 11/11] update file --- ciphers/beaufort_cipher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ciphers/beaufort_cipher.py b/ciphers/beaufort_cipher.py index 3d42da90d79a..c885dec74001 100644 --- a/ciphers/beaufort_cipher.py +++ b/ciphers/beaufort_cipher.py @@ -51,7 +51,7 @@ def cipher_text(message: str, key_new: str) -> str: # and returns the original text def original_text(cipher_text: str, key_new: str) -> str: """ - >>> originalText("BDC PAYUWL JPAIYI","SECRETSECRETSECRE") + >>> original_text("BDC PAYUWL JPAIYI","SECRETSECRETSECRE") 'THE GERMAN ATTACK' """ or_txt = ""