Last active
March 16, 2022 20:27
-
-
Save Sigmanificient/f05e8508f2df708cab06a15fc19e2f1e to your computer and use it in GitHub Desktop.
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
from string import ascii_lowercase | |
from time import perf_counter | |
from typing import Tuple, Generator, List | |
CHARSET_LENGTH = len(ascii_lowercase) | |
Pair = Tuple[int, int] | |
Key = Tuple[int, int, int, int] | |
def encode_pair(pair: Pair, key: Key) -> Pair: | |
a, b, c, d = key | |
l, r = pair | |
return ( | |
(a * l + b * r) % CHARSET_LENGTH, | |
(c * l + d * r) % CHARSET_LENGTH | |
) | |
def brute_force(target: str, key: Key) -> str: | |
def paired_chunks(lst: Tuple[int, ...]) -> Generator[Tuple[int, ...], None, None]: | |
for i in range(0, len(lst), 2): | |
yield tuple(lst[i:i + 2]) | |
_combinations = tuple( | |
(left, right) | |
for left in range(CHARSET_LENGTH) | |
for right in range(CHARSET_LENGTH) | |
) | |
word: List[int] = [] | |
indexes = tuple(ascii_lowercase.index(c) for c in target) | |
for pair in paired_chunks(indexes): | |
for combination in _combinations: | |
if pair == encode_pair(combination, key): | |
word.extend(combination) | |
return ''.join(ascii_lowercase[i] for i in word) | |
if __name__ == '__main__': | |
print( | |
brute_force( | |
'fpdgxant', | |
(11, 3, 7, 4) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment