caesar cipher - StackingFlowing/CTProgramming GitHub Wiki

Encryption

Caesar cipher is probably the most classic way used to encrypt a message.

We start by writing down our message. We will also prepare a list of all alphabet.

the enemy is coming (this is our message to be encrypted)

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

We will choose a number that determines how we swap out the letters. Let's choose 2 as our number of offset.

The first letter in the message is t. We now look for the letter in the alphabet and count to 2.

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
                                      ^ 1 2

If we count to 2 from t, we get v in the alphabetical order, so we will replace t with v. Our message now becomes

vhe enemy is coming

If we repeat this process for all letters, we will get the fully encrypted message: vjg gpgoa ku eqokpi

Decryption

Of course, if we want to decrypt it at any time, we just do the reverse: count the same amount of offset, but in the opposite direction.

The first letter in the encrypted message vjg gpgoa ku eqokpi is v.

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
                                      2 1 ^

If we count from v and go back 2 letters, we get t. So we will re-replace h with g.

Repeating this for all letters gives us the enemy is coming

...which is the original message!

There is a major issue with this message, however. It is too easy! our enemies could decode it easily by trying all possible offsets.

To keep our message safe, here are some ways that we can use to improve our cipher.

  • increase the number of shifts. For example, a corresponds to m, b corresponds to n, c corresponds to o, and so on. The number of shifts is now 12.
  • randomise the order of the alphabet. For example, instead of using "abcdefghijklmnopqrstuvwxyz" we can use "qwertyyuiopasdfghjklzxcvbnm".
  • include numbers, symbols and upper case letters. It will be more difficult to decode if "a" corresponds to "3" and "b" corresponds to "#", etc.
⚠️ **GitHub.com Fallback** ⚠️