-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Add Bifid cipher #5493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add Bifid cipher #5493
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
643eaad
Add Bifid cipher
cordeirossauro eebd7a1
Add missing type hint
cordeirossauro e29c606
Fix variable names
cordeirossauro ac03b99
Merge branch 'TheAlgorithms:master' into master
cordeirossauro 1b24fa6
Merge branch 'TheAlgorithms:master' into master
cordeirossauro 6513a1d
Merge branch 'TheAlgorithms:master' into master
cordeirossauro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
#!/usr/bin/env python3 | ||
|
||
""" | ||
The Bifid Cipher uses a Polybius Square to encipher a message in a way that | ||
makes it fairly difficult to decipher without knowing the secret. | ||
|
||
https://www.braingle.com/brainteasers/codes/bifid.php | ||
""" | ||
|
||
import numpy as np | ||
|
||
|
||
class BifidCipher: | ||
def __init__(self) -> None: | ||
SQUARE = [ | ||
["a", "b", "c", "d", "e"], | ||
["f", "g", "h", "i", "k"], | ||
["l", "m", "n", "o", "p"], | ||
["q", "r", "s", "t", "u"], | ||
["v", "w", "x", "y", "z"], | ||
] | ||
self.SQUARE = np.array(SQUARE) | ||
|
||
def letter_to_numbers(self, letter: str) -> np.ndarray: | ||
""" | ||
Return the pair of numbers that represents the given letter in the | ||
polybius square | ||
|
||
>>> np.array_equal(BifidCipher().letter_to_numbers('a'), [1,1]) | ||
True | ||
|
||
>>> np.array_equal(BifidCipher().letter_to_numbers('u'), [4,5]) | ||
True | ||
""" | ||
index1, index2 = np.where(self.SQUARE == letter) | ||
indexes = np.concatenate([index1 + 1, index2 + 1]) | ||
return indexes | ||
|
||
def numbers_to_letter(self, index1: int, index2: int) -> str: | ||
""" | ||
Return the letter corresponding to the position [index1, index2] in | ||
the polybius square | ||
|
||
>>> BifidCipher().numbers_to_letter(4, 5) == "u" | ||
True | ||
|
||
>>> BifidCipher().numbers_to_letter(1, 1) == "a" | ||
True | ||
""" | ||
letter = self.SQUARE[index1 - 1, index2 - 1] | ||
return letter | ||
|
||
def encode(self, message: str) -> str: | ||
""" | ||
Return the encoded version of message according to the polybius cipher | ||
|
||
>>> BifidCipher().encode('testmessage') == 'qtltbdxrxlk' | ||
True | ||
|
||
>>> BifidCipher().encode('Test Message') == 'qtltbdxrxlk' | ||
True | ||
|
||
>>> BifidCipher().encode('test j') == BifidCipher().encode('test i') | ||
True | ||
""" | ||
message = message.lower() | ||
message = message.replace(" ", "") | ||
message = message.replace("j", "i") | ||
|
||
first_step = np.empty((2, len(message))) | ||
for letter_index in range(len(message)): | ||
numbers = self.letter_to_numbers(message[letter_index]) | ||
|
||
first_step[0, letter_index] = numbers[0] | ||
first_step[1, letter_index] = numbers[1] | ||
|
||
second_step = first_step.reshape(2 * len(message)) | ||
encoded_message = "" | ||
for numbersIndex in range(len(message)): | ||
cordeirossauro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
index1 = int(second_step[numbersIndex * 2]) | ||
index2 = int(second_step[(numbersIndex * 2) + 1]) | ||
letter = self.numbers_to_letter(index1, index2) | ||
encoded_message = encoded_message + letter | ||
|
||
return encoded_message | ||
|
||
def decode(self, message: str) -> str: | ||
""" | ||
Return the decoded version of message according to the polybius cipher | ||
|
||
>>> BifidCipher().decode('qtltbdxrxlk') == 'testmessage' | ||
True | ||
""" | ||
message = message.lower() | ||
message.replace(" ", "") | ||
first_step = np.empty(2 * len(message)) | ||
for letter_index in range(len(message)): | ||
numbers = self.letter_to_numbers(message[letter_index]) | ||
first_step[letter_index * 2] = numbers[0] | ||
first_step[letter_index * 2 + 1] = numbers[1] | ||
|
||
second_step = first_step.reshape((2, len(message))) | ||
decoded_message = "" | ||
for numbersIndex in range(len(message)): | ||
cordeirossauro marked this conversation as resolved.
Show resolved
Hide resolved
cordeirossauro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
index1 = int(second_step[0, numbersIndex]) | ||
index2 = int(second_step[1, numbersIndex]) | ||
letter = self.numbers_to_letter(index1, index2) | ||
decoded_message = decoded_message + letter | ||
|
||
return decoded_message |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.