roofline.devPublic Beta

Matrix Decrypt-Encrypt

0MEDIUMRankedReport issue

A Hill cipher is a cipher based on matrix multiplication. To encrypt a message c‾\underline{c} of length ≤1n≤4096\leq 1 n \leq 4096 in an alphabet of size 1≤l≤655351 \leq l \leq 65535, pick some key AA of size n×nn \times n and compute c‾A=Ac‾mod  l\underline{c}_A = A\underline{c} \mod l. To decrypt the message, apply A−1c‾A=(A−1A)c‾=Ic‾=c‾A^{-1}\underline{c}_A = (A^{-1}A)\underline{c} = I\underline{c} = \underline{c}.

For this problem, given a message c‾A\underline{c}_A (encrypted for AA), the encryption key AA, and the encryption key BB, rekey the message c‾A\underline{c}_A to become c‾B\underline{c}_B.

The input cAc_A will be supplied in c. The final output cBc_B must also be placed in c.

Input

  • A - original encryption key.
  • B - target encryption key.
  • n - size of the Hill cipher key matrices.
  • l - alphabet modulus.
  • c - ciphertext encrypted under A, provided in place.

Output

  • c - the input vector modified in place to become c‾B\underline{c}_B.
Open on a desktop browser to write and submit code.