From c8f87d4c59469c43cc485a825807c29cb4175285 Mon Sep 17 00:00:00 2001 From: PotatoK123 <56174807+PotatoK123@users.noreply.github.com> Date: Sun, 11 Oct 2020 11:45:07 +0100 Subject: [PATCH 1/3] Added rail fence cipher --- ciphers/rail_fence_cipher.py | 99 ++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 ciphers/rail_fence_cipher.py diff --git a/ciphers/rail_fence_cipher.py b/ciphers/rail_fence_cipher.py new file mode 100644 index 000000000000..f4680f2b196b --- /dev/null +++ b/ciphers/rail_fence_cipher.py @@ -0,0 +1,99 @@ +def encrypt(input_string: str, key: int) -> str: + """ + Shuffles the character of a string by placing each of them + in a grid (the height is dependant on the key) in a zigzag + formation and reading it left to right. + + >>> encrypt("Hello World", 4) + 'HWe olordll' + + >>> encrypt("This is a message", 0) + Traceback (most recent call last): + ... + ValueError: Height of grid can't be 0 or negative + + >>> encrypt(b"This is a byte string", 5) + Traceback (most recent call last): + ... + TypeError: sequence item 0: expected str instance, int found + """ + grid = [[] for _ in range(key)] + lowest = key - 1 + + if key <= 0: + raise ValueError("Height of grid can't be 0 or negative") + if key == 1 or len(input_string) <= key: + return input_string + + for position, character in enumerate(input_string): + num = position % (lowest * 2) # puts it in bounds + num = min(num, lowest * 2 - num) # creates zigzag pattern + grid[num].append(character) + grid = ["".join(row) for row in grid] + output_string = "".join(grid) + + return output_string + + +def decrypt(input_string: str, key: int) -> str: + """ + Generates a template based on the key and fills it in with + the characters of the input string and then reading it in + a zigzag formation. + + >>> decrypt("HWe olordll", 4) + 'Hello World' + + >>> decrypt("This is a message", -10) + Traceback (most recent call last): + ... + ValueError: Height of grid can't be 0 or negative + + >>> decrypt("My key is very big", 100) + 'My key is very big' + """ + grid = [] + lowest = key - 1 + + if key <= 0: + raise ValueError("Height of grid can't be 0 or negative") + if key == 1: + return input_string + + temp_grid = [[] for _ in range(key)] # generates template + for position in range(len(input_string)): + num = position % (lowest * 2) # puts it in bounds + num = min(num, lowest * 2 - num) # creates zigzag pattern + temp_grid[num].append("*") + + counter = 0 + for row in temp_grid: # fills in the characters + splice = input_string[counter : counter + len(row)] + grid.append([character for character in splice]) + counter += len(row) + + output_string = "" # reads as zigzag + for position in range(len(input_string)): + num = position % (lowest * 2) # puts it in bounds + num = min(num, lowest * 2 - num) # creates zigzag pattern + output_string += grid[num][0] + grid[num].pop(0) + return output_string + + +def bruteforce(input_string: str) -> dict: + '''Uses decrypt function by guessing every key + + >>> bruteforce("HWe olordll")[4] + 'Hello World' + ''' + results = {} + for key_guess in range(1, len(input_string)): # tries every key + results[key_guess] = decrypt(input_string, key_guess) + return results + + +if __name__ == "__main__": + import doctest + + doctest.testmod() \ No newline at end of file From 7e5244063fe294a1dd4ea99706c924d8f9ab3887 Mon Sep 17 00:00:00 2001 From: PotatoK123 <56174807+PotatoK123@users.noreply.github.com> Date: Sun, 11 Oct 2020 11:49:56 +0100 Subject: [PATCH 2/3] Update rail_fence_cipher.py --- ciphers/rail_fence_cipher.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ciphers/rail_fence_cipher.py b/ciphers/rail_fence_cipher.py index f4680f2b196b..524fa96645a2 100644 --- a/ciphers/rail_fence_cipher.py +++ b/ciphers/rail_fence_cipher.py @@ -1,3 +1,5 @@ +''' https://en.wikipedia.org/wiki/Rail_fence_cipher ''' + def encrypt(input_string: str, key: int) -> str: """ Shuffles the character of a string by placing each of them @@ -96,4 +98,4 @@ def bruteforce(input_string: str) -> dict: if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + doctest.testmod() From efe6200b803ae09873043d15265cd7b69492e44e Mon Sep 17 00:00:00 2001 From: PotatoK123 <56174807+PotatoK123@users.noreply.github.com> Date: Sun, 11 Oct 2020 11:57:41 +0100 Subject: [PATCH 3/3] Update rail_fence_cipher.py --- ciphers/rail_fence_cipher.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/ciphers/rail_fence_cipher.py b/ciphers/rail_fence_cipher.py index 524fa96645a2..2596415207ae 100644 --- a/ciphers/rail_fence_cipher.py +++ b/ciphers/rail_fence_cipher.py @@ -1,9 +1,10 @@ -''' https://en.wikipedia.org/wiki/Rail_fence_cipher ''' +""" https://en.wikipedia.org/wiki/Rail_fence_cipher """ + def encrypt(input_string: str, key: int) -> str: """ Shuffles the character of a string by placing each of them - in a grid (the height is dependant on the key) in a zigzag + in a grid (the height is dependent on the key) in a zigzag formation and reading it left to right. >>> encrypt("Hello World", 4) @@ -26,7 +27,7 @@ def encrypt(input_string: str, key: int) -> str: raise ValueError("Height of grid can't be 0 or negative") if key == 1 or len(input_string) <= key: return input_string - + for position, character in enumerate(input_string): num = position % (lowest * 2) # puts it in bounds num = min(num, lowest * 2 - num) # creates zigzag pattern @@ -61,19 +62,19 @@ def decrypt(input_string: str, key: int) -> str: raise ValueError("Height of grid can't be 0 or negative") if key == 1: return input_string - + temp_grid = [[] for _ in range(key)] # generates template for position in range(len(input_string)): num = position % (lowest * 2) # puts it in bounds num = min(num, lowest * 2 - num) # creates zigzag pattern temp_grid[num].append("*") - + counter = 0 for row in temp_grid: # fills in the characters splice = input_string[counter : counter + len(row)] grid.append([character for character in splice]) counter += len(row) - + output_string = "" # reads as zigzag for position in range(len(input_string)): num = position % (lowest * 2) # puts it in bounds @@ -84,11 +85,11 @@ def decrypt(input_string: str, key: int) -> str: def bruteforce(input_string: str) -> dict: - '''Uses decrypt function by guessing every key + """Uses decrypt function by guessing every key >>> bruteforce("HWe olordll")[4] 'Hello World' - ''' + """ results = {} for key_guess in range(1, len(input_string)): # tries every key results[key_guess] = decrypt(input_string, key_guess)