Having Fun With Simple Ciphers

Posted on Mar 11, 2021 - 0 min read.
19 likes!

I was recently looking into learning more about certain algorithms, but one GitHub repo led to another and I instead ended up learning about some simple ciphers used throughout human history.

The history of cryptography is fascinating. It is thousands of years old; Egyptians used it some 4000 years ago. But it wasn't until the 19th century that cryptography began to mature, both in encryption and cryptanalysis (the science of finding weaknesses in crypto systems). I highly recommend checking out the History of Cryptography Wikipedia page.

I wanted to use this blog post as a way to learn how to implement some of the simpler cryptography methods in JavaScript, and create interactive examples with React components. Let's get started 🎉

Caesar Cipher

History

The first technique I'll cover is the Caesar cipher, named after Julius Caesar, one of the simplest. The Caesar cipher, also known as the Caesar shift, is a type of substitution cipher where every letter in your message is replaced by another letter that is some offset number of spots in the same alphabet. So if your offset is -3 the letter D would become the letter A.

While this may have been a suitable level of encryption 2000 years ago, the same did not hold true for the 20th century. Russians attempted to use the Caesar Cipher during World War I in lieu of more sophisticated methods. This was largely because it was easier for their troops to decode messages in the field, but the same was true for German and Austrian cryptanalysts that got a hold of those "secret" messages.

Example

Use the slider in the example below to alter the offset and encrypt the sample text.

Your offset: 0
  • 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
  • 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

What makes the Caesar cipher fun is its simplicity. You can easily encrypt and decrypt messages with pencil and paper, so long as you know what the offset is. Keep it secret! 😂

Playfair Cipher

Now we take it up a level. The Playfair Cipher uses a symmetric-key algorithm. These are algorithms for cryptography that use the same cryptographic keys for both the encryption of plaintext and the decryption of ciphertext.

History

Invented in 1854 by Charles Wheatstone, it was used during the first and second World Wars by the British. While this cipher can still easily be decrypted with pencil and paper, it is a bit more involved. Thus, this was used in battle to pass along important (albeit not top secret) messages. These messages usually were relevant for a short period of time and were irrelevant by the time the enemy was able to decrypt them.

Encryption Rules

There are a number of factors that make this cipher interesting. It uses a key phrase and a key table to encrypt pairs of letters in a message.

Start by coming up with a short phrase as your key and place each letter, from left to right (or another chosen pattern) on a 5x5 grid. Make sure to skip duplicate letters. Fill the rest of the grid with the alphabet, also skipping any letters already on the grid. We now have our key table ready to encrypt our super secret message 🕵️

Next, you take your super secret message and break it down to digrams (pairs of letters), so a phrase like "Hello world" would become "HE LL OW OR LD". You then use the key table to substitute the digrams with their encrypted counterparts.

Using this table you can now encrypt pairs of letters from your message following another set of rules:

  • If the letters appear on the same row, replace them with the letters to their right and loop back around if they're at the edge.
  • If the letters appear on the same column, replace them with the letters below and loop back to the top if they're at the bottom.
  • If the letters are not on the same row or column, replace each one with the letters on the same row but at the column of the other (forming a square or rectangle).

There's a couple of caveats that also come into play to make sure you can effectively encrypt a message. Your message has to have an even number of letters, and your digrams can't consist of repeating letters. Repeating letters are broken up by inserting an X, and a message with an uneven number of letters can get a Q appended to it (or some other non frequent letter).

To see this in action hover over the pairs of letters below to see their encrypted counterparts highlighted.

Monarchy
We hold these truths to be self-evident
  • WE
  • HO
  • LD
  • TH
  • ES
  • ET
  • RU
  • TH
  • ST
  • OB
  • ES
  • EL
  • FE
  • VI
  • DE
  • NT
M
O
N
A
R
C
H
Y
B
D
E
F
G
I
K
L
P
Q
S
T
U
V
W
X
Z
  • UG
  • FH
  • TC
  • PD
  • IL
  • KL
  • MZ
  • PD
  • TL
  • AH
  • IL
  • LU
  • GF
  • XF
  • CK
  • RQ
UGFHTCPDILKLMZPDTLAHILLUGFXFCKRQ

There is such a variety of ciphers out there! I'll be following up this blog post with a further deep dive into a several more. Thanks for reading! 🙌

  1. #programming
...

Thanks for reading! Share this post on Twitter if you enjoyed it!