SouthParkMe

Hi.

This site tends to hold geeky things.

Monoalphabetic Ciphers

Monoalphabetic Ciphers

I previously wrote about a ‘Caesar shift’ cipher, where every letter is replaced by a letter that is a certain shift down the alphabet.

In a mono-alphabetic cipher, we swap each letter with another, but in a consistent way. If the letter T is replaced by a J, it is always replaced by a J.

 PLAIN: ABCDEFGHIJKLMNOPQRSTUVWXYZ
CIPHER: SNYIQGVPTBRCFHZKLMWJXOUEDA

The pattern SNY etcetera is called the ‘key’.

Thus, we get text like this:

“Shi hzu, vqhjcqfqh,” wsti i’Smjsvhsh, utjpzxj wjzkkthv jz qekcsth ptw yzhixyj jz Kzmjpzw, “Scc gzm zhq, zhq gzm scc--jpsj tw zxm fzjjz, tw tj hzj?”

“Shi dqj--” wsti Kzmjpzw.

“Pzci zxj dzxm pshi shi wuqsm!” ymtqi Sjpzw shi Smsftw sj zhyq.

Zoqmyzfq nd qesfkcq, vmxfncthv jz ptfwqcg, hqoqmjpqcqww, Kzmjpzw wjmqjypqi zxj ptw pshi, shi jpq gzxm gmtqhiw mqkqsjqi utjp zhq oztyq jpq gzmfxcs ityjsjqi nd i’Smjsvhsh:

“Scc gzm zhq, zhq gzm scc.”

“Jpsj’w uqcc! Hzu cqj xw qoqmdzhq mqjtmq jz ptw zuh pzfq,” wsti i’Smjsvhsh, sw tg pq psi izhq hzjpthv nxj yzffshi scc ptw ctgq; “shi sjjqhjtzh! Gzm gmzf jptw fzfqhj uq smq sj gqxi utjp jpq ysmithsc.”

There are many ways in which the swaps can be done. There are 26 ways to swap the first letter, and that leaves 25 ways to swap the second, and 24 ways to swap the third.

So just for A, B and C there are 26 × 25 × 24 or 15 600 options. For the whole alphabet there are 26 × 25 × 24 × … × 3 × 2 × 1 options. Mathematicians write this 26!

26! is pronounced 26 factoral, the ! means multiply this number by each smaller number until you get to 1.

26! is a huge number, 4.03 × 10²⁶, that is a 4 followed by 26 zeros.

This is a seriously large number.

If you were to start counting one item each second, starting at the big bang and continuing to now, you’d not scratch the surface - you’d have to repeat this exercise a thousand million times to even get close.

This seems pretty secure, yes?

No.

A monoalphabetic cipher is not broken by guessing the ‘key’…. but that is a story for another day.

I’ve put some code below.

Recent Cryptography Posts

More Cryptography

Extension

The code for generating the above is a small piece of Python 3. It randomly encrypts a text file called inputtext.txt - the text file should be stored in the same folder as the code.

import random

def randomcipher():
    """ this generates a randomised cipher alphabet """
    cipher=[]
    print(' PLAIN:',end=' ')
    for i in range(26):
        cipher.append(i)
        print(chr(65+i),end='')
    for i in range(100):
        i=random.randint(0,25)
        j=random.randint(0,25)
        k=cipher[i]
        cipher[i]=cipher[j]
        cipher[j]=k
    print()
    print('CIPHER:',end=' ')
    for i in range(26):
        print(chr(65+cipher[i]),end='')
    print()
    return(cipher)

cipher=randomcipher()

# Read the input, and put each line in a list of lines!
filetimetable = open("inputtext.txt","r")
lines=filetimetable.readlines()
filetimetable.close()

output=""

for line in lines:
    newline=""
    for letter in line:
        asc=ord(letter)
        # let us only deal with letters
        if (64 < asc < 91) or ( 96 < asc < 123):
            if (asc > 96):
                base=97
            else:
                base=65
            # I now have a number from 0 to 25
            lett=asc-base
            # let's look up the cipher
            subst=cipher[lett]
            # and let's convert back to letter
            letter=chr(base+subst)
        newline+=letter
    output+=newline

print(output)
Four Fours

Four Fours

Luhn Algorithm

Luhn Algorithm