Implementation of classical encryption techniques in Java

Introduction

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.

Ceaser Cipher

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

Mathematical representation:

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:

Encryption Equation

c = E(p, k) = (p+k) mod 26

Decryption Equation

p = D(c, k) = (c-k) mod 26

Example:

Encrypt the message "ceasar encryption algorithm"

Plaintext ceasar encryption algorithm
p 24018017 41321724151981413 01161417819712
(p+k) mod 26 57321320 71652011822111716 3149172011221015
Ciphertext fhdvdu hqfubswlrq dojrulwkp
(c-k) mod 26 24018017 41321724151981413 01161417819712
Recovered message ceasar encryption algorithm

Java code:

Output :

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

Monoalphabetic Cipher

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 abcdefghijklmnopqrstuvwxyz
Key: rnxviepcqzhsuogtwdljymfbka

Example:

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

Plaintext monoalphabetic encryption algorithm
Key: ugogrstcrnijqx ioxdktjqgo rspgdqjcu

Java code:

Output :

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)

Playfair Cipher

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.

Rules for encryption :

  1. 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}.
  2. 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.
  3. 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.
  4. 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.

Rules for decryption :

  1. Ciphertext is divided into pairs of letters.
  2. 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.
  3. 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.
  4. 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.

Example:

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 playfairencryptionalgorithmx
Ciphertext: tkhahiocncryeaiotrtsqgcoadqw

Java code:

Output :

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)

Subscribe to Our Mailing List



Share:

Subscribe


Blog Tags

Follow me

Facebook Page