[Math] How to programmatically find the inverse of a 2×2 matrix (mod 26).

cryptographymatricesmodules

I'm trying to create a hill cipher utility. One feature I want is to be able to compute the key if you have the plaintext and ciphertext.

$C$ = ciphertext matrix ($2\times 2$), $P$ = plaintext matrix $\left(2\times\frac{N}{2}\right)$, $K$ = key ($2\times 2$).

$$PK = C$$

So if I have $C$ and $P$, then $K = P^{-1}C$. How do I best find $P^{-1}$?

EDIT: It is important to note that this is all mod $26$. Sorry.

Best Answer

You could use the direct formula for $2 \times 2$:

$$ \pmatrix{a & b\\ c & d}^{-1} = \dfrac{1}{ad-bc}\pmatrix{d & -b\\-c & a}$$

Related Question