Thursday 19 August 2010

Ciphers

Running on from the previous article about codes, I thought I'd write some more, this time about ciphers.

A cipher differs from a code, in that a cipher replaces letters, or numbers, with another, whereas a code can replace many letters or words with just one. For example, the phrase "Meet me tonight" can be replaced by the code 1254, or "blue", or whatever. If we encypher the message, however, by replacing the individual letters with the key to the right on the keyboard we get "<rry ,r ypmohjy". This is known as a substitution cipher. The other type of cipher is called transposition. This is simply taking the letters of the message and re-arranging them, as in an anagram:

M M N T
E E I X
E T G Y
T O H Z

Reading the letters vertically produces the message, with a little extra letters to make a square number. Horizontally produces the code, MMNT EEIX ETGY TOHZ.

Digital Ciphers

Instead of working on letters of the alphabet, digital ciphers work on bits, or blocks of bits, usually using an XOR function. For example, convert the message to ASCII:

M e e t
m e
77 101 101 116 32 109 101
1001101 1100101 1100101 1110100 0100000 1101101 1100101

The XOR function changes the bits such that unequal bits produce a 1, while equal bits produce a 0. If we now use an ASCII key phrase:

Plaintext M e e t
m e
ASCII (Decimal) 77 101 101 116 32 109 101
Binary 1001101 1100101 1100101 1110100 0100000 1101101 1100101
Keytext S e n d
t h
ASCII (Decimal) 83 101 110 100 32 116 104
Binary 1010011 1100101 1101110 1100100 0100000 1110100 1101000
XOR Result 0011110 0000000 0001011 0010000 0000000 0011001 0001101
Decimal 30 0 11 16 0 25 13

The result of XORing is a series of mostly unprintable ASCII characters, but the stream now bears no relation to the original message. For example, the three "e"'s in the message are now all different, 0, 11, and 13.

The mechanism above is roughly how a Stream Cipher works. Instead of a key phrase, the cipher generates an endless stream of seemingly random bits which are XOR'd with the plaintext stream to produce the cipher stream.

No comments:

Post a Comment