Created
May 1, 2023 19:20
-
-
Save kamaci/972378b61460acc382d31f3d3d4f77e3 to your computer and use it in GitHub Desktop.
This file contains 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
import textwrap | |
class PermutationCipher: | |
def __init__(self, permutation): | |
self.permutation = permutation | |
self.permutation_transpose = [0] * len(self.permutation) | |
for index, value in enumerate(self.permutation): | |
self.permutation_transpose[value] = index | |
def encrypt(self, plain_text): | |
return self._permutation(plain_text, self.permutation) | |
def decrypt(self, cipher_text): | |
return self._permutation(cipher_text, self.permutation_transpose) | |
@staticmethod | |
def _permutation(text, permutation): | |
text = text.upper() | |
return "".join( | |
"".join(text_bucket[value] for value in permutation) | |
for text_bucket in textwrap.wrap(text, width=len(permutation)) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment