In this post, I will explain classical encryption techniques Ceasar cipher, Monoalphabetic cipher, and Playfair cipher and provide a java code demonstrating encryption and decryption processes for each one.

It is an old encryption technique developed by Julius Ceasar for hiding the content of a message by substituting each letter in a message with a letter existing three or more places down or up in the same alphabet.

Next table shows an example for shift = 3.
Note that the alphabet is rotated such that the letter after Z is A.

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 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|

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 |

Each letter in the alphabet is represented by equivalent letter such as A is assigned to 0 and Z to 25.

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 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|

0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 |

For each plaintext letter p in the message is substituted by ciphertext letter c with shift = k such that k takes values from 1 to 25:

Encrypt the message "ceasar encryption algorithm"

Plaintext | c | e | a | s | a | r | e | n | c | r | y | p | t | i | o | n | a | l | g | o | r | i | t | h | m | ||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|

p | 2 | 4 | 0 | 18 | 0 | 17 | 4 | 13 | 2 | 17 | 24 | 15 | 19 | 8 | 14 | 13 | 0 | 11 | 6 | 14 | 17 | 8 | 19 | 7 | 12 | ||

(p+k) mod 26 | 5 | 7 | 3 | 21 | 3 | 20 | 7 | 16 | 5 | 20 | 1 | 18 | 22 | 11 | 17 | 16 | 3 | 14 | 9 | 17 | 20 | 11 | 22 | 10 | 15 | ||

Ciphertext | f | h | d | v | d | u | h | q | f | u | b | s | w | l | r | q | d | o | j | r | u | l | w | k | p | ||

(c-k) mod 26 | 2 | 4 | 0 | 18 | 0 | 17 | 4 | 13 | 2 | 17 | 24 | 15 | 19 | 8 | 14 | 13 | 0 | 11 | 6 | 14 | 17 | 8 | 19 | 7 | 12 | ||

Recovered message | c | e | a | s | a | r | e | n | c | r | y | p | t | i | o | n | a | l | g | o | r | i | t | h | m |

run: Plaintext message : ceasar encryption algorithm Ciphertext message: fhdvdu hqfubswlrq dojrulwkp Recovered message : ceasar encryption algorithm BUILD SUCCESSFUL (total time: 0 seconds)

Before explaining this cipher, letâ€™s define what permutation is. Permutation is an ordered sequence of all elements in a finite set of element S, with each element appears exactly once. For example, if S={1,2,3}, there are 3! = 6 permutations 123, 132, 213, 231, 312, 321.

Suppose that S is the english alphabet, so there are 26! permutations. These permutations can be an arbitrary substitutions providing more security rather than Ceasar cipher which has only 25 keys. Encryption is done by maping each letter in the message with correspondent letters in the key and decryption is done by reversing operation.

Next table shows english alphabet with one of these permutations (Key).

Plaintext | 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 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|

Key: | r | n | x | v | i | e | p | c | q | z | h | s | u | o | g | t | w | d | l | j | y | m | f | b | k | a |

Encrypt the message "monoalphabetic encryption algorithm" with monoalphabetic cipher using the previous key.

Plaintext | m | o | n | o | a | l | p | h | a | b | e | t | i | c | e | n | c | r | y | p | t | i | o | n | a | l | g | o | r | i | t | h | m | ||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|

Key: | u | g | o | g | r | s | t | c | r | n | i | j | q | x | i | o | x | d | k | t | j | q | g | o | r | s | p | g | d | q | j | c | u |

run: Plaintext : monoalphabetic encryption algorithm Alphabet : abcdefghijklmnopqrstuvwxyz key : rnxviepcqzhsuogtwdljymfbka Ciphertext message: ugogrstcrnijqx ioxdktjqgo rspgdqjcu Recovered message : monoalphabetic encryption algorithm BUILD SUCCESSFUL (total time: 0 seconds)

The Playfair algorithm is based on the use of a 5 * 5 matrix of letters constructed using a keyword. This matrix is filled by keyword letters from left to right and then filling the reminders places in the matrix with the remaining letters in alphabetic order. Letters I and J are considered one letter.

- Message is divided into pairs of letters, if a pair has same letter, separate between then by x, such as tree {tr, ee} >>{tr, ex, ex}.
- Two plaintext letters that fall in the same row of the matrix are each replaced by the letter to the right, with the first element of the row circularly following the last. For example, ry is encrypted as ye.
- Two plaintext letters that fall in the same column are each replaced by the letter beneath, with the top element of the column circularly following the last. For example, eu is encrypted as pe.
- Otherwise, each plaintext letter in a pair is replaced by the letter that lies in its own row and the column occupied by the other plaintext letter. Such as pl >> tk.

- Ciphertext is divided into pairs of letters.
- Two ciphertext letters that fall in the same row of the matrix are each replaced by the letter to the left, with the first element of the row circularly following the last. For example, ye is encrypted as ry.
- Two ciphertext letters that fall in the same column are each replaced by the letter above, with the top element of the column circularly following the last. For example, pe is encrypted as eu.
- Otherwise, each ciphertext letter in a pair is replaced by the letter that lies in its own row and the column occupied by the other ciphertext letter. Such as tk >> pl.

Encrypt the message "playfair encryption algorithm" with playfair cipher using the key "encryption".

**Generated matrix **

e | n | c | r | y |

p | t | i / j | o | a |

b | d | f | g | h |

k | l | m | q | S |

u | v | w | x | z |

Plaintext | pl | ay | fa | ir | en | cr | yp | ti | on | al | go | ri | th | mx |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|

Ciphertext: | tk | ha | hi | oc | nc | ry | ea | io | tr | ts | qg | co | ad | qw |

run: Plaintext : playfair encryption algorithm Alphabet : abcdefghiklmnopqrstuvwxyz with key : encryption Generated matrix : encry ptioa bdfgh klmqs uvwxz Encrypted message: tkhahiocncryeaiotrtsqgcoadqw Recovered message: playfairencryptionalgorithmx BUILD SUCCESSFUL (total time: 0 seconds)

Share: