# Apply a permutation cipher by hand

I did the following problem from the book "Cryptography Theory and Practice" as I am doing some self-study.

Problem:

a) Suppose that $\pi$ is the following permutations of the set $\lbrace 1,\dots,8 \rbrace$: $$\pi=\begin{pmatrix} 1 &2& 3& 4& 5& 6& 7& 8\\ 4& 1& 6& 2& 7& 3& 8& 5 \end{pmatrix}$$
Compute the permutation $\pi^{-1}$.

b) Decrypt the following cipher text, for a permutation Cipher with $m = 8$, which was encrypted using the key $\pi$:

${\bf TGEEMNLENNTDROEOAAHDOETCSHAEITLM.}$

a)$$\pi^{-1}=\begin{pmatrix} 1 &2& 3& 4& 5& 6& 7& 8\\ 2& 4& 6& 1& 8& 3& 5& 7 \end{pmatrix}$$

b) To decrypt, I partition the text into groups of 8 giving.

${\bf TGEEMNLE\ \ NNTDROEOA\ \ AHDOETCS\ \ HAEITLM.}$

${\bf TGEEMNLE}$ decrpyts to ${\bf ETNGLEEM.}$

I am fairly sure that this is wrong. However, I do not know what I did wrong. Please help.

Bob

• FWIW: I agree with your $\pi^{-1}$ and I get the same "decryption" assuming it works via blocks as claimed. Did you try the other blocks? Isn't it a columnar transposition? Feb 9, 2017 at 2:25
• I think the book commits an error in the example it gives, so that's why you think that $\pi^{-1}$ is the encryption function. Nov 30, 2021 at 11:58

Apply the $\pi^{-1}$ to all the blocks. Then put them under each other so we get colums of length 4 in this case. Then read out left to right, up to down instead (zig-zag).

Your $\pi^{-1}$ is correct but for decryption you can do this:

$\pi^{-1}\begin{pmatrix} T&G&E&E&M&N&L&E\\ 1&2&3&4&5&6&7&8\\ N&N&T&D&R&O&E&O\\ 1&2&3&4&5&6&7&8\\A&A&H&D&O&E&T&C\\ 1&2&3&4&5&6&7&8\\ S&H&A&E&I&T&L&M\\ 1&2&3&4&5&6&7&8\\ \end{pmatrix}= \begin{matrix} G&E&N&T&E&E&M&L\\2&4&6&1&8&3&5&7\\N&D&O&N&O&T&R&E\\2&4&6&1&8&3&5&7\\A&D&E&A&C&H&O&T\\2&4&6&1&8&3&5&7\\H&E&T&S&M&A&I&L\\2&4&6&1&8&3&5&7\\ \end{matrix}.$

So decrypted message is: ${\bf GENTEEML\ \ NDONOTRE\ \ ADEACHOT\ \ HETSMAIL.}$

Note that ${\bf TGEEMNLE}$ encrypts to ${\bf ETNGLEEM}$, Not decrypts to.

Just to make everything sure, first of all, we define a permutation in the following way:

$$\pi \colon X\to X$$

this is, for any $$x \in X$$, we have an unique corresponding $$x' \in X$$ such that, $$\pi(x) = x'$$, a function that rearranges the same elements of the domain to a new form (called a permutation), and the function is said to be a bijection.

Now, $$x$$ is called a plaintext (a letter to a friend, or a gmail, for example) and $$x'$$ is called a ciphertext (the plaintext encrypted using our function $$\pi$$). So, indeed, we have the following:

$$\pi=\begin{pmatrix} 1 &2& 3& 4& 5& 6& 7& 8\\ 4& 1& 6& 2& 7& 3& 8& 5 \end{pmatrix}$$

Observe now that, the following is going to happen: $$\pi(1) = 4$$ (this is, the letter of plaintext in position 1 is going to be moved to the position 4 in the ciphertext), $$\pi(2)$$ (this is, the letter of plaintext in position 2 is going to be moved to the position 1 in the ciphertext), and so on. That's why, the encryption of the given plaintext: TGEEMNELNNTDROEOAAHDOETCSHAEIRLM is what the above answer tells us. Observe that since $$m = 8$$, we have to divide everything in blocks of 8 letters, and also, the algorithm follows:

def decrypt(cipher, ciphertext):
return encrypt(inverse_key(cipher), ciphertext)

def encrypt(cipher, plaintext):
plaintext = "".join(plaintext.split(" ")).upper()
ciphertext = ""
plaintext += "X"
for offset in range(0, len(plaintext), len(cipher)):
for element in [a-1 for a in cipher]:
ciphertext += plaintext[offset+element]
ciphertext += " "
return ciphertext[:-1]

def inverse_key(cipher):
inverse = []
for position in range(min(cipher),max(cipher)+1,1):
inverse.append(cipher.index(position)+1)
return inverse

cipher = [2,4,1,5,3]
plaintext = "LOREM IPSUM DOLOR SITAM ETCON SECTE TUERA DIPIS CINGE LITXX"
ciphertext = encrypt(cipher, plaintext)

#cipher = [2,4,1,5,3]
#ciphertext = "OELMR PUIMS OODRL IASMT TOENC ETSEC URTAE IIDSP IGCEN IXLXT"
#plaintext = decrypt(cipher, ciphertext)

print(ciphertext)