Introduction

Overview

Substitution

Letters of plain text are replaced by other
letters or by numbers or symbols

If the plain text is viewed as a sequence of bits, then
substitution involves replacing plain text bit patterns with cipher text bit
patterns

Transposition/Permutation

The letters/bytes/bits of the plaintext are
rearranged without altering the actual letters used

Can be easily recognized since the ciphertext have the same frequency
distribution as the original plain text.

Substitution Algorithms

Caesar Cipher

Earliest known substitution cipher by Julius Caesar. It involves replacing each letter in the plaintext by a
shifted letter in the alphabet used. Example: If the shift value is (3) then we can define transformation as:

Mono-alphabetic Cipher

Cryptanalysis

Now the Brute Force attack to this cipher requires exhaustive search of a total of 26! = 4 x 1026 keys, but the cryptanalysis makes use of the language characteristics, the Letter that is commonly used in English is the letter e , then T,R,N,I,O,A,S
other letters are fairly rare
Z,J,K,Q,X
There are tables of single, double & triple letter frequencies:

Playfair Cipher

One approach to improving security was to encrypt multiple letters, Playfair Key Matrix:
A 5X5 matrix of letters based on a keyword
Fill in letters of keyword (sans duplicates), Fill rest of matrix with other letters.

Example:
Using "playfair example" as the key, the table becomes:

P

L

A

Y

F

I

R

E

X

M

B

C

D

G

H

K

N

O

Q

S

T

U

V

W

Z

Plaintext encrypted two letters at a
time

If a pair is a repeated letter, insert
a filler like 'X', ex: “Communication" encrypts as “Com
x munication"

If the letters appear on the same row of your table, replace them with the letters to their immediate right respectively (wrapping around to the left side of the row if a letter in the original pair was on the right side of the row).

XM becomes MI

If the letters appear on the same column of your table, replace them with the letters immediately below respectively (again wrapping around to the top side of the column if a letter in the original pair was on the bottom side of the column).

NU becomes UL

If the letters are not on the same row or column, replace them with the letters on the same row respectively but at the other pair of corners of the rectangle defined by the original pair. The order is important – the first letter of the encrypted pair is the one that lies on the same row as the first letter of the plaintext pair.

TH becomes ZB

To decrypt, use the INVERSE (opposite) of the last 3 rules, and the 1st as is (dropping any extra "X"s that don't make sense in the final message when you finish).

Encrypting the message "Hide the gold in the tree stump":

Cipher Text:
BM OD ZB XD NA BE KU DM UI XM MO UV IF

The Playfair cipher is a great advance
over simple monoalphabetic ciphers, due to:

The identification of digrams is more
difficult than individual letters:

i) In the Monoalphabetic cipher, the
attacker searches in 26 letters only.

ii) But using the Playfair cipher, the
attacker is searching in 26 x 26 = 676 digrams.

The relative frequencies of individual
letters exhibit a much greater range than that of digrams, making frequency
analysis much more difficult.

Hill Cipher

Each letter is first encoded as a number. Often the simplest
scheme is used: A = 0, B =1, ..., Z=25, but this is not an
essential feature of the cipher.
The encryption takes m successive plaintext letter and
substitutes them for m ciphertext letters.
In case m = 3 , the encryption can be expressed in terms of
the matrix multiplication as follows:

Mathematical Model:

C = EK (P) = KP mod 26

P = DK (C ) = K-1 C mod 26 = K-1 KP = P
K is the key matrix and K-1 is the matrix inverse.
The inverse matrix can be calculated as K.K-1 = I
where I is the identity matrix.

Polyalphabetic Ciphers

Another approach to improving security is to use multiple cipher alphabets
Called polyalphabetic substitution ciphers.
Makes cryptanalysis harder with more alphabets to guess and flatter frequency distribution.

Use a key to select which alphabet is used for each letter of the message.

Example: the Vigenère Cipher:

Write the plain text

Write the keyword repeated below it

Use each key letter as a
Caesar cipher key

Encrypt the corresponding plaintext letter.

Decryption works in the opposite way

Cryptanalysis:

The problem in repeating the key so frequently, is that there might be repetitions in the ciphertext.
This helps in the cryptanalysis process as it gives a clue on the key period.

Ideally we want a key as long as the message, this is done in Autokey Cipher.

Autokey Cipher

Vigenère proposed the autokey cipher to strengthen his cipher system.
With keyword is prefixed to message as key
But still have frequency characteristics to attack.

key: deceptivewearediscoveredsav

plaintext: wearediscoveredsaveyourself

ciphertext: ZICVTWQNGKZEIIGASXSTSLVVWLA

Notice how we completed the key with characters from plain text.

One-Time Pad

This is a system that uses a truly random key
as long as the message in the decryption and encryption
processes.
It is unbreakable since ciphertext bears no statistical
relationship to the plaintext since for any plaintext & any
ciphertext there exists a key mapping one to other.

This system is practically infeasible since its impractical to
generate large quantities of random keys.
The key distribution and protection is a big problem in this
case.

Transposition Ciphers

Rail Fence cipher

Write the message letters out diagonally over a number of rows then read off cipher row by row.

Example:
Encrypting the following message using rail fence of depth 2:

Product Ciphers

Ciphers using substitutions or transpositions are not secure because of language characteristics.
Hence consider using several ciphers in succession to make cryptanalysis harder,
two substitutions make a more complex substitution, two transpositions make more complex transposition
but a substitution followed by a transposition makes a new much harder cipher.

Comments and Discussions

Very helpful article.
I'm actually working on my project based on encryption & decryption techniques. So I just wanted to ask if I can use this source code for my project. I'm gonna add some of my ideas too. I won't copy the whole thing but just the logic & maybe lil piece of code, of course with your permission.

Thank you, I was trying to focus on the main goal, implementing various methods of text encryption.
That's why i kept validations to a minimum, instead of catering for each and every corner case that's not really relevant to the topic.

Is it possible to get the fixed length encrypted string output from the algorithm ?
Because i will give 24 character input, and i want 64 character Encrypted output and the output should be Decrypted also..So please give me the code for this...

Hi, Can these article and sample be extended to support either alphanumeric string or may be simply numeric-only text? Are there any specific Encryption on numeric where out can be generated into alphabetic and/or alphanumeric text?