Skip to content

Instantly share code, notes, and snippets.

@lior5654
Created April 9, 2022 21:16
Show Gist options
  • Save lior5654/aa3a38fc14928628b8bfd0b661d27a16 to your computer and use it in GitHub Desktop.
Save lior5654/aa3a38fc14928628b8bfd0b661d27a16 to your computer and use it in GitHub Desktop.
Solution to PlaidCTF 2022 Process Sample | by H4K47L4N1M
# made by H4K47L4N1M | @lior5654
from typing import Text, List
from sage.all import *
# Constants
ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_!?"
INV_ALPHABET = {c: i for i, c in enumerate(ALPHABET)}
N = len(ALPHABET)
R = IntegerModRing(N)
SAMPLE_ID = "_LLPEK!M!FHL?CPI?SBSHCYYH"
ENC = "XKGHB_LRYIHUC?CC_BUPWUJ?S"
SECRET_LEN = 25
def text_to_sequence(text: Text) -> Text:
return [INV_ALPHABET[c] for c in text]
def sequence_to_text(sequence: List[int]) -> Text:
return [ALPHABET[x] for x in sequence]
def recover(sample_id: Text, enc: Text) -> Text:
sample_id = text_to_sequence(sample_id)
enc = text_to_sequence(enc)
res = []
block_matrix = [
[sample_id[t] for t in range(i, 25, 5)] for i in range(0, 5)
]
block_matrix = Matrix(R, block_matrix).T
for row_index in range(0, 5):
goal_vector = vector(R, [enc[i] for i in range(row_index, 25, 5)])
for v in block_matrix.solve_right(goal_vector):
res.append(v)
return "".join(sequence_to_text(res))
def main():
print(recover(SAMPLE_ID, ENC))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment