1.0.0 • Published 6 years ago

@cryptoolsorg/caesarcipher v1.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

Caesar Cipher

History and usage

The Caesar Cipher was named after Julius Caesar (100 B.C. – 44 B.C). He would use the cipher for secret communication (protect messages of military significance). The Caesar Cipher is a substitution cipher. Originally, Julius Caesar would use a shift of three to encrypt/decrypt a message. The Caesar Cipher encrypts a message using an affine function : f(x) = 1x + b.

Detailed Explanations : How it works?

  1. Firstly, each character of the initial text (message to encrypt) is converted in a number from 0 to 25, corresponding to its position in the Latin alphabet which contains 26 letters --> (a = 0, b = 1 ... z = 25 ).

  2. Then, each number obtained is transformed by an affine function (f(x) = 1x + b). "x" is representing the number while "b" is defined during the encryption. "b" is the key used to decrypt the final message.

  3. If we take all the images and put them in a list, we obtain n numbers corresponding to n characters of the initial text. The next step consists in finding the values of modulo 26 of each number. (Modulo means remainder)

Example : Modulo 4 of 19 is 3 because 15 = 4 4 + 3 In the other hand, modulo 26 of 26 is 0 because 26 = 26 1 + 0

  1. Therefore, we obtain a new list with n element, each between 0 and 25 both included. All these numbers are converted in letters of the Latin Alphabet using the tables below.

  2. We finally create the final message by putting all the letters side by side.

Steps 1 and 4 can be done with these tables :

ABCDEFGHIJKLM
0123456789101112
NOPQRSTUVWXYZ
13141516171819202122232425

Weaknesses

  • If an attacker knows that the message has been encrypted using Caesar Cipher, he can try all shifts (b values from 1 to 25) to decrypt the message. This is called the bruteforce method.

  • We can also use frequency analysis to decrypt the message as each letter is encrypted with the same algorithm and the most common letters in english are :

Example

Encrypting

  • Message to encrypt : ZATTACKZ
  • Shift used : 4 (f(x) = 1x + 4)
  • That means that b = 4

Using the above tables, ATTACK can be written as : 25 0 19 19 0 2 10 25 Images of each number :

  • f(25) = 29
  • f(0) = 4
  • f(19) = 23
  • f(2) = 6
  • f(10) = 14

The new list is : 29 4 23 23 4 6 14 29

Using the modulo 26 method, we obtain:

  • Mod(29,26) = 3
  • Mod(4,26) = 4
  • Mod(23,26) = 23
  • Mod(6,26) = 6
  • Mod(14,26) = 14

The final message is 3 4 23 23 4 6 14 3 and using the tables again, we convert them in the encrypted message :

DEXXEGOD

ZATTACKZ is encrypted with the function x + 4 and becomes DEXXEGOD.

Decrypting

First method : Knowing the key (value of the shift used)

  • Message to decrypt : DEXXEGOD
  • Shift used : 4 (f(x) = 1x - 4)
  • That means that b = -4

Using the above tables, DEXXEGOD can be written as : 3 4 23 23 4 6 14 3 Images of each number :

  • f(3) = -1
  • f(4) = 0
  • f(23) = 19
  • f(6) = 2
  • f(14) = 10

The new list is : -1 0 19 19 0 2 10 -1

Using the modulo 26 method, we obtain :

  • Mod(-1,26) = 25
  • Mod(0,26) = 0
  • Mod(19,26) = 19
  • Mod(2,26) = 2
  • Mod(10,26) = 10

The final message is 25 0 19 19 0 2 10 25 and using the tables again, we convert them in the encrypted message :

ZATTACKZ

DEXXEGOD is decrypted with the function 1x - 4 and becomes ZATTACKZ.

Second method : Not knowing the key (value of the shift used)

This is called the bruteforce method.

  • Message to decrypt : DEXXEGOD

Using the above tables, DEXXEGOD can be written as : 3 4 23 23 4 6 14 3

a is a number between 0 and 25. (a = 0 would mean the message is already decrypted)

Using the function f(x) = Mod(1x + a, 26) :

We can get all these results :

aDecrypted text
1fgzzgiqf
2ghaahjrg
3hibbiksh
4ijccjlti
5jkddkmuj
6kleelnvk
7lmffmowl
8mnggnpxm
9nohhoqyn
10opiiprzo
11pqjjqsap
12qrkkrtbq
13rsllsucr
14stmmtvds
15tunnuwet
16uvoovxfu
17vwppwygv
18wxqqxzhw
19xyrryaix
20yzsszbjy
21zattackz
22abuubdla
23bcvvcemb
24cdwwdfnc
25dexxegod

The only text that makes sense is zattackz so we can deduce that the key is 21 (25 - b = 21).

DEXXEGOD is decrypted with the function f(x) = 1x - 4 or f(x) = 1x + 21 and becomes ZATTACKZ.