Skip to content

Instantly share code, notes, and snippets.

@kamaci
Created May 1, 2023 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamaci/972378b61460acc382d31f3d3d4f77e3 to your computer and use it in GitHub Desktop.
Save kamaci/972378b61460acc382d31f3d3d4f77e3 to your computer and use it in GitHub Desktop.
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