CryptoNugget

Cryptography is a minor hobby of author Richard Heathfield. He has written this article in much the same way that he might have written for his book, C Unleashed, and he hopes you'll find it diverting and send you to the book for more!

From the author of

From the author of

When I was writing the C
Unleashed outline, I knew that I wouldn’t be able to write the whole
book myself but I didn’t know who the co-authors would be and, consequently,
I didn’t know which chapters they would be able to take off my hands. Sams
was very keen to have a chapter on cryptography in the book. So, until Mike
Wright turned up (bless him!), I was faced with the very real possibility that
I might have to write the chapter myself. I’m no expert in cryptography,
but it is a minor hobby of mine and when I was approached for a “nugget”
- something not specifically in the book, but which might interest readers,
it seemed appropriate to include an article I wrote a while back. I've edited
it in much the same way that I might have edited it for the actual book. Naturally
this isn’t going to be a full-blown 50-page chapter, but I hope you’ll
find it diverting, anyway.

Elementary Cryptography

Let’s begin by looking at a simple cipher - a substitution cipher.
This cipher substitutes each letter of the alphabet with a different one. For
the purposes of this article we will consider plaintexts consisting entirely
of upper case letters, to simplify matters. The techniques shown here can be
easily modified for plaintexts using a computer’s entire character set
or any subset thereof.

Perhaps the most common form of substitution cipher is ROT13, which
is typically available on Unix systems (on my Linux system it’s called
Caesar). In ROT13 each letter is rotated 13 places around the alphabet.
Thus, HELLO becomes WTAAD.

We can represent this as follows:

P is the plaintext (in this case, HELLO). C is the ciphertext (in this case,
WTAAD). If we call the process of substituting ROT13, then C = ROT13(P).

In this case, to decrypt the message is simple. We just ROT13 it again. Think
of a dial with an indicator pointing upwards. If you move it 180 degrees clockwise,
it now points downwards. Turn it 180 degrees clockwise again, and it points
upwards again. Thus, P = ROT13(C) and, therefore, P = ROT13(ROT13(C)).

As you might imagine, this is not a particularly difficult cipher to crack.
It is sometimes used on Usenet to allow people to read information, if they
so choose, by deciphering it. For example: for all those who can’t wait
for the final episode of “Dying For A Drink,” I can exclusively reveal
(ROT13): GUR OHGYRE QVQ VG. As you can see, those using ROT13 will frequently
publish the fact to assist people who want to decrypt the ciphertext. ROT13
is not intended to be a super-secure cipher.

Programming ROT13 is relatively trivial. Here’s C code to do it and which
assumes the ASCII character set is being used:

ROT13 is, not surprisingly, easy to crack. Nevertheless, by using different
rearrangements of letters some people think they can achieve security. For example,
here is a substitution cipher that is possibly a shade more secure than ROT13:

ABCDEFGHIJKLMNOPQRSTUVWXYZ

QWERTYUIOPASDFGHJKLZXCVBNM

The cipher works like this: for each character in the plaintext, find that
character in the top row. Look at the character beneath it: this is the corresponding
character in the ciphertext. Decryption, of course, comprises the opposite operation
(look it up in the second row, convert to the character above).

Thus, HELLOWORLD becomes ITSSGVGKSR

This seems a little more secure, no doubt. Unfortunately, it’s not. A
short message makes it a little harder, but not much. It is possible, and indeed
trivial, to cryptanalyse substitution ciphers using letter frequencies. You
can easily determine which letters are used frequently in a given language using
a simple program to count alphabetic characters in a large sample of text. You
can then compare that table against your ciphertext and make deductions about
it. According to one analysis, the frequency of English letters is (from most
common to least common) ETAONRISHDLFCMUGPYWBVKXJQZ. Your mileage may vary: it
obviously depends on the text sample you use. Nevertheless, E is a clear winner,
and every study I ever saw puts T second and A/O third/fourth (sometimes the
other way around - O/A). Between them, these four letters account for approximately
40% of all letters used!

There is more information yet to be gained from a monoalphabetical cipher -
letter patterns. For example, consider the phrase “letter pattern”.
Take each word in turn, and assign each unique letter in the word a number.
“Letter” gives us “123324” (1 = L, 2 = E, etc). “Pattern”
gives us “1233456”.

There aren’t that many words in the English language that give us these
patterns. Some duplicates exist, for example, “LET” and “CAT”
have the same pattern codes, but longer words can give us useful pattern information
which we can use to help us crack a monoalphabetic cipher.

The following is a sample of C code that takes a list of words, one per line,
and prints out their patterns. It’s not amazingly efficient - in fact it
has a time complexity of O(m2 * n) where n is the number
of words and m is the average number of letters in a word. Still, it’s
better than nothing. It was actually written with a Unix system’s /usr/dict/words
dictionary in mind (with the idea of building a pattern dictionary for decryption)
but could be simply adapted to serve as a cryptanalytic tool (and was designed
with this flexibility in mind).

As a result of these trivial cracks, substitution ciphers are not at all secure
- frequency analysis of any ciphertext of 40 characters or more is unlikely
to fail to reveal the plaintext.

A former colleague, in a former lifetime, was very keen on substitution ciphers.
For some reason he thought that if he ran his plaintext through successive substitution
tables, some of which were numbers rather than letters, and some of which were
invented symbols, it would somehow be really secure. He presented his ‘uncrackable’
ciphertext to me proudly and it took me about 5 minutes to crack. He asked me
how I’d managed to deduce the existence of the intermediate tables. My
answer was “What intermediate tables?” I’d had no idea he’d
gone to all that trouble! The existence of the intermediate tables was irrelevant
to the crack, because there was still a one-to-one mapping between the plaintext
and the ciphertext.

Homophonic substitution is a variation on the same theme. In homophonic substitution,
each character of plaintext can be replaced by one of a small selection of ciphertext
characters. So you could, for example, map A to 54, 90, 102, or 155; B to 2,
37, 39 or 158; C to 17, 38, 70 or 99; etc. This is a lot harder to crack, but
it’s still not impossible. The same statistical irregularities of the plaintext
will still show up and, thus, decryption is possible.

Maybe we can do better by encrypting groups of letters instead of individual
letters. This certainly gives us more scope. If we take groups of three characters
at a time, that gives us approximately 17000 (I’ll let you cube 26 for
yourselves!) possible groups of plaintext triplets. If we mapped each to a unique
ciphertext triplet, that would be a lot harder to crack, yes? For example, you
can encipher AAA as FOO, AAB as BAR, AAC as QUX, AAD as FRE, etc.

Well, okay, it’s better. It’s still not very good, though. For sufficiently
large samples of ciphertext it is still possible to get a handle on statistical
anomalies in the data. For example, what’s the betting that THE is the
most common triplet in the English language? (Not counting the capitalised word,
the triplet THE still occurs three times in that last sentence alone.) So all
you’d have to do is find the most common triplet and you have a hook into
the cipher. I’m not saying that THE is necessarily the most common, although
it is a good guess. Writing a program to determine a triplet frequency table
is left as an exercise for the discerning reader.

Okay, how about using more than one substitution table? Maybe if we had, say,
six tables, and we used the first table to encrypt the first, seventh, thirteenth
letters of the plaintext, and the second to encrypt the second, eighth, and
fourteenth, etc?

Not a bad idea, it seems, but all this really means is that it takes more ciphertext
before the tables can be deduced (in this case, only six times as much ciphertext).
Computer programs exist which can do this kind of cryptanalysis extraordinarily
quickly and accurately.

But "WAIT!," I hear you cry. Any one of these methods could have
been used. The cryptanalyst has no way of knowing which encryption
method I’ve used! So how can one possibly decide which cryptanalytical
technique to use on my ciphertext?

Three answers exist for this. Firstly, if your security relies on the secrecy
of your algorithm, that’s not security, it’s obscurity. Your algorithm
must be known to at least two people - the sender and the receiver of the information.
If you have a group of people who all need to share secure information, you’re
going to have to change your algorithm every time somebody leaves the group,
because they know the secret and now they are a 'loose cannon', so to speak.

Secondly, even if it’s just the two of you, and even if you have complete
confidence that your secret algorithm won’t be revealed (unwittingly, deliberately,
or under coercion) by the other person, that still doesn’t help. Cryptanalysts
know a huge number of algorithms and have a large selection of cryptanalysis
programs at their disposal.

Thirdly, if you are using a “secret” computer program to encrypt
and decrypt your secrets, remember that cryptanalysts are bright bunnies. If
they can get hold of the binary of your program (to how many people have you
distributed this binary, hmmm?), they can disassemble it and study your algorithm.
In fact, it’s a rather perverse truth of cryptography that the truly secure
algorithms are those which have been published by their authors and subjected
to all kinds of attacks by some of the best cryptanalysts in the world. Anything
which can survive that onslaught, intact, mustbe good.

So, if you used any of the techniques I’ve described so far and a professional
cryptanalyst got hold of your ciphertext, I wouldn’t give your secret ten
minutes before it was cracked.