how to crack a random substitution text cypher?

Question:

I was reading about Caesar cipher where the characters are simply shifted by a number like this:

l=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

def shift(l,n):
    res = l[n:] +l[:n]
    return res

We can then move the list 2 steps to the right, for example, to get:

l_c2= ['c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b']

For encrypting the message one just has to substitute each character in the original text with the shifted one. This method is very easy to break, because once you know the encoded version of one character, you know all the others, and even if we do not, we can try 26 shiftings to find the correct shift: it’s a small number of tests!

So I was thinking if I randomly reorder the elements of the list with:

import random

def randomReorder(l):
    return random.sample(l,len(l))

Then I will get a list that looks like this:

l_r = ['f', 'e', 'l', 'r', 'p', 't', 'k', 'v', 'u', 'c', 'd', 'o', 'a', 'x', 'm', 'g', 'b', 'z', 'q', 's', 'h', 'j', 'i', 'n', 'w', 'y']

So if I subsitute the letters in the original text with these ones, if one know the key to one character, it’s hard to predict the others, because they are simpley randomly reordered,
so for "hello", for example, it become "vpoom". Because the cipher list is just random, so a cracker will have to test many reordered lists to find the list which can give a "more english" result, which are 10^26 possible arrangments. So can this method of encrypting data be powerful?, or there is something that I’m missing that crackers can use to break the ciphering?

Asked By: A.M.M Elsayed

||

Answers:

Yes, true if your text can be anything but natural languages have a lot of redundancy and aren’t random.

Decrypting a random string, encrypted with random substitution might be hard to decrypt.

But since the text is English, you can do frequency analysis e.g. the most commonly occurring letter is "e"; "j" is the least commonly occurring, and so on. You can also think about character pairs (e.g. "th" occurs a lot), or character triplets and so on.

Answered By: Danyal
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.