Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 20

Cryptography refers to the science and art of transforming messages to make them secure

and immune to attacks. It is a method of storing and transmitting data in a particular form so
that only those for whom it is intended can read and process it. Cryptography not only
protects data from theft or alteration but can also be used for user authentication.
Components
There are various components of cryptography which are as follows −
Plaintext and Ciphertext
The original message, before being transformed, is called plaintext. After the message is
transformed, it is called ciphertext. An encryption algorithm transforms the plaintext into
ciphertext; a decryption algorithm transforms the ciphertext back into plaintext. The sender
uses an encryption algorithm, and the receiver uses a decryption algorithm.
Cipher
We refer to encryption and decryption algorithms as ciphers. The term cipher is also used to
refer to different categories of algorithms in cryptography. This is not to say that every
sender-receiver pair needs their very own unique cipher for secure communication. On the
contrary, one cipher can serve millions of communicating pairs.
Key
A key is a number (or a set of numbers) that the cipher, as an algorithm, operates on. To
encrypt a message, we need an encryption algorithm, an encryption key, and plaintext. These
create the ciphertext. To decrypt a message, we need a decryption algorithm, a decryption
key, and the ciphertext. These reveal the original plaintext.
Types
There are two types of cryptography which are as follows −
Symmetric Key Cryptography
In symmetric-key cryptography, the same key is used by both parties. The sender uses this
key and an encryption algorithm to encrypt data; the receiver uses the same key and the
corresponding decryption algorithm to decrypt the data.
Asymmetric-Key Cryptography
In asymmetric or public-key cryptography, there are two keys: a private key and a public
key. The private key is kept by the receiver. The public key is announced to the public.
In public-key encryption/decryption, the public key that is used for encryption is different
from the private key that is used for decryption. The public key is available to the public, and
the private key is available only to an individual.
Symmetric Key Cryptography-
 
In this technique,
 Both sender and receiver uses a common key to encrypt and decrypt the message.
 This secret key is known only to the sender and to the receiver.
 It is also called as secret key cryptography.
 
Working-
 
The message exchange using symmetric key cryptography involves the following steps-

 Before starting the communication, sender and receiver shares the secret key.
 This secret key is shared through some external means.
 At sender side, sender encrypts the message using his copy of the key.
 The cipher text is then sent to the receiver over the communication channel.
 At receiver side, receiver decrypts the cipher text using his copy of the key.
 After decryption, the message converts back into readable format.
Symmetric Encryption Algorithms-
 Some of the encryption algorithms that use symmetric key are-
 Advanced Encryption Standard (AES)
 Data Encryption Standard (DES)
Asymmetric Key Cryptography-
 In this technique,
 Sender and receiver use different keys to encrypt and decrypt the message.
 It is called so because sender and receiver use different keys.
 It is also called as public key cryptography.
Working-
 The message exchange using public key cryptography involves the following steps-

Step-01:
 
At sender side,
 Sender encrypts the message using receiver’s public key.
 The public key of receiver is publicly available and known to everyone.
 Encryption converts the message into a cipher text.
 This cipher text can be decrypted only using the receiver’s private key.
 
Step-02:
 The cipher text is sent to the receiver over the communication channel.
 Step-03:
 At receiver side,
 Receiver decrypts the cipher text using his private key.
 The private key of the receiver is known only to the receiver.
 Using the public key, it is not possible for anyone to determine the receiver’s private
key.
 After decryption, cipher text converts back into a readable format.

 
Symmetric Encryption Algorithms-
 Some of the encryption algorithms that use symmetric key are-
 Advanced Encryption Standard (AES)
 Data Encryption Standard (DES)

Data encryption standard (DES) 


Data encryption standard (DES) has been found vulnerable to very powerful attacks and
therefore, the popularity of DES has been found slightly on the decline. DES is a block
cipher and encrypts data in blocks of size of 64 bits each, which means 64 bits of plain text
go as the input to DES, which produces 64 bits of ciphertext. The same algorithm and key
are used for encryption and decryption, with minor differences. The key length is  56 bits.
The basic idea is shown in the figure:

We have mentioned that DES uses a 56-bit key. Actually, the initial key consists of 64 bits.
However, before the DES process even starts, every 8th bit of the key is discarded to
produce a 56-bit key. That is bit positions 8, 16, 24, 32, 40, 48, 56, and 64 are discarded.  
Thus, the discarding of every 8th bit of the key produces a 56-bit key from the original 64-
bit key.
DES is based on the two fundamental attributes of cryptography: substitution (also called
confusion) and transposition (also called diffusion). DES consists of 16 steps, each of
which is called a round. Each round performs the steps of substitution and transposition.
Let us now discuss the broad-level steps in DES. 
 In the first step, the 64-bit plain text block is handed over to an initial
Permutation (IP) function.
 The initial permutation is performed on plain text.
 Next, the initial permutation (IP) produces two halves of the permuted block;
saying Left Plain Text (LPT) and Right Plain Text (RPT).
 Now each LPT and RPT go through 16 rounds of the encryption process.
 In the end, LPT and RPT are rejoined and a Final Permutation (FP) is performed
on the combined block
 The result of this process produces 64-bit ciphertext.
Initial Permutation (IP):  
As we have noted, the initial permutation (IP) happens only once and it happens before the
first round. It suggests how the transposition in IP should proceed, as shown in the figure.
For example, it says that the IP replaces the first bit of the original plain text block with the
58th bit of the original plain text, the second bit with the 50th bit of the original plain text
block, and so on.
This is nothing but jugglery of bit positions of the original plain text block. the same rule
applies to all the other bit positions shown in the figure.

As we have noted after IP is done, the resulting 64-bit permuted text block is divided into
two half blocks. Each half-block consists of 32 bits, and each of the 16 rounds, in turn,
consists of the broad-level steps outlined in the figure. 

Step-1: Key transformation:  


We have noted initial 64-bit key is transformed into a 56-bit key by discarding every 8th bit
of the initial key. Thus, for each a 56-bit key is available. From this 56-bit key, a different
48-bit Sub Key is generated during each round using a process called key transformation.
For this, the 56-bit key is divided into two halves, each of 28 bits. These halves are
circularly shifted left by one or two positions, depending on the round.
For example: if the round numbers 1, 2, 9, or 16 the shift is done by only one position for
other rounds, the circular shift is done by two positions. The number of key bits shifted per
round is shown in the figure.

After an appropriate shift, 48 of the 56 bits are selected. for selecting 48 of the 56 bits the
table is shown in the figure given below. For instance, after the shift, bit number 14 moves
to the first position, bit number 17 moves to the second position, and so on. If we observe
the table carefully, we will realize that it contains only 48-bit positions. Bit number 18 is
discarded (we will not find it in the table), like 7 others, to reduce a 56-bit key to a 48-bit
key. Since the key transformation process involves permutation as well as a selection of a
48-bit subset of the original 56-bit key it is called Compression Permutation.

Step-2: Expansion Permutation:  


Recall that after the initial permutation, we had two 32-bit plain text areas called Left Plain
Text(LPT) and Right Plain Text(RPT). During the expansion permutation, the RPT is
expanded from 32 bits to 48 bits. Bits are permuted as well hence called expansion
permutation. This happens as the 32-bit RPT is divided into 8 blocks, with each block
consisting of 4 bits. Then, each 4-bit block of the previous step is then expanded to a
corresponding 6-bit block, i.e., per 4-bit block, 2 more bits are added. 
This process results in expansion as well as a permutation of the input bit while creating
output. The key transformation process compresses the 56-bit key to 48 bits. Then the
expansion permutation process expands the 32-bit RPT to 48-bits. Now the 48-bit key is
XOR with 48-bit RPT and the resulting output is given to the next step, which is the  S-Box
substitution.

Advanced Encryption Standard (AES)


Advanced Encryption Standard (AES)  is a specification for the encryption of electronic
data established by the U.S National Institute of Standards and Technology (NIST) in 2001.
AES is widely used today as it is a much stronger than DES and triple DES despite being
harder to implement.
Points to remember
 AES is a block cipher.
 The key size can be 128bits.
 Encrypts data in blocks of 128 bits each.
That means it takes 128 bits as input and outputs 128 bits of encrypted cipher text as output.
AES relies on substitution-permutation network principle which means it is performed
using a series of linked operations which involves replacing and shuffling of the input data.
Working of the cipher:
AES performs operations on bytes of data rather than in bits. Since the block size is 128
bits, the cipher processes 128 bits (or 16 bytes) of the input data at a time.
The number of rounds depends on the key length as follows:
 128 bit key – 10 rounds

Creation of Round keys:

A Key Schedule algorithm is used to calculate all the round keys from the key. So the

initial key is used to create many different round keys which will be used in the

corresponding round of the encryption.

Encryption:
AES considers each block as a 16 byte (4 byte x 4 byte = 128 ) grid in a column major
arrangement.

[ b0 | b4 | b8 | b12 |
| b1 | b5 | b9 | b13 |
| b2 | b6 | b10| b14 |
| b3 | b7 | b11| b15 ]
Each round comprises of 4 steps :
 SubBytes
 ShiftRows
 MixColumns
 Add Round Key
The last round doesn’t have the MixColumns round.
The SubBytes does the substitution and ShiftRows and MixColumns performs the
permutation in the algorithm.
SubBytes:
This step implements the substitution.
In this step each byte is substituted by another byte. Its performed using a lookup table also
called the S-box. This substitution is done in a way that a byte is never substituted by itself
and also not substituted by another byte which is a compliment of the current byte. The
result of this step is a 16 byte (4 x 4 ) matrix like before.
The next two steps implement the permutation.
ShiftRows :
This step is just as it sounds. Each row is shifted a particular number of times.
 The first row is not shifted
 The second row is shifted once to the left.
 The third row is shifted twice to the left.
 The fourth row is shifted thrice to the left.
(A left circular shift is performed.)

[ b0 | b1 | b2 | b3 ] [ b0 | b1 | b2 | b3 ]
| b4 | b5 | b6 | b7 | -> | b5 | b6 | b7 | b4 |
| b8 | b9 | b10 | b11 | | b10 | b11 | b8 | b9 |
[ b12 | b13 | b14 | b15 ] [ b15 | b12 | b13 | b14 ]

MixColumns:
This step is basically a matrix multiplication. Each column is multiplied with a specific
matrix and thus the position of each byte in the column is changed as a result.
This step is skipped in the last round.
[ c0 ] [ 2 3 1 1 ] [ b0 ]
| c1 | = | 1 2 3 1 | | b1 |
| c2 | | 1 1 2 3 | | b2 |
[ c3 ] [ 3 1 1 2 ] [ b3 ]
Add Round Keys :
Now the resultant output of the previous stage is XOR-ed with the corresponding round
key. Here, the 16 bytes is not considered as a grid but just as 128 bits of data.

After all these rounds 128 bits of encrypted data is given back as output. This process is
repeated until all the data to be encrypted undergoes this process.
Decryption:
The stages in the rounds can be easily undone as these stages have an opposite to it which
when performed reverts the changes. Each 128 blocks goes through the 10,12 or 14 rounds
depending on the key size.
The stages of each round in decryption is as follows:
 Add round key
 Inverse MixColumns
 ShiftRows
 Inverse Sub Byte
The decryption process is the encryption process done in reverse so i will explain the steps
with notable differences.
Inverse MixColumns:
This step is similar to the MixColumns step in encryption, but differs in the matrix used to
carry out the operation.
[ b0 ] [ 14 11 13 9 ] [ c0 ]
| b1 | = | 9 14 11 13 | | c1 |
| b2 | | 13 9 14 11 | | c2 |
[ b3 ] [ 11 13 9 14 ] [ c3 ]

Inverse SubBytes:
Inverse S-box is used as a lookup table and using which the bytes are substituted
during decryption.
RSA ALGORITHM:
The RSA algorithm is an asymmetric cryptography algorithm; this means that it uses
a public key and a private key (i.e two different, mathematically linked keys). As their
names suggest, a public key is shared public, while a private key is secret and must not
be shared with anyone.
The RSA algorithm is named after those who invented it in 1978: Ron Rivest, Adi
Shamir, and Leonard Adleman.
The following illustration highlights how asymmetric cryptography works:
Example : Key generation
1. Select two prime numbers p and q (for the purpose of security)P=3 and q=5
2. Calculate n=p*q
=3*5
=15
3. Calculate Φ(n)=(p-1)(q-1)
=(3-1)(5-1)
=2*4=8
4. Choose the value of ‘e’ (encryption)such that

gcd(greatest common divisor) gcd (e ,Φ(n))=1


gcd (e,8)=1
gcd(3,8)=1
E=3

5. Calculate d=e1mod Φ(n)


ed=1mod Φ(n)
edmodΦ(n)=1
3*d mod 8=1
3*3mod 8=1
9mod8=1 i.e 1= 1
d=3
6. Public key = {e, n} = {3,15}
7.Privatekey = {d, n} = {3,15}

Encryption: c = me mod n
=43mod 15 m= no of digits in plain text m=4(assume)
=64 mod 15
=4

Decryption: m=cd mod n


=43 mod 15
=64 mod 15
=4
Substitution technique
Substitution technique is a classical encryption technique where the characters present in
the original message are replaced by the other characters or numbers or by symbols. If
the plain text (original message) is considered as the string of bits, then the substitution
technique would replace bit pattern of plain text with the bit pattern of cipher text.

1. Caesar Cipher
2. Playfair Cipher
3. One-Time Pad

Transposition technique 

Transposition technique is an encryption method which is achieved by


performing permutation over the plain text. Mapping plain text into cipher text using
transposition technique is called transposition cipher.

1.Rail Fence Transposition

2.Columnar Transposition

Caesar cipher
The Caesar Cipher technique is one of the earliest and simplest methods of encryption
technique. It’s simply a type of substitution cipher, i.e., each letter of a given text is
replaced by a letter with a fixed number of positions down the alphabet. For example, with
a shift of 1, A would be replaced by B, B would become C, and so on.
Replacing each letter of the alphabet with the letter standing three places futher down the
alphabet.
Algorithm:
For each plaintext letter ‘p’, substitute the ciphertext letter ‘C’
C = E(p,k) mod 26
= (p+k) mod 26
P = D(C,k) mod 26
=(C- k) mod 26
Example :
Palain text = “ H E L L O”
Key=3
H E L L O
k H O O R

Solution : Step 2: Find the C(E)

Step 1: Find the C(H) C(E) = (p+K) mod 26

C(H) = (p+K) mod 26 =(4+3)mod 26

=(8+3)mod 26 =7mod26

=11mod26 =7=H

=11=k
Step 4: Find the C(L)
Step 3: Find the C(L)
C(L) = (p+K) mod 26
C(L) = (p+K) mod 26
=(11+3)mod 26
=(11+3)mod 26
=14mod26
=14mod26
=14=O
=14=O
Step 5: Find the C(O)
C(O) = (p+K) mod 26
=(14+3)mod 26
=17 mod26
=17=R
Playfair Cipher
Playfair cipher is an encryption algorithm to encrypt or encode a message. It is the same as a
traditional cipher. The only difference is that it encrypts a digraph (a pair of two letters)
instead of a single letter.

Table rules: 

Rule 1: It initially creates a key-table of 5*5 matrix.


Note that any alphabet should not be repeated.
Another point to note that there are 26 alphabets and we have only 25 blocks to put a letter
inside it. So, I and j merge one cell.
Rule 2: The plaintext is split into pairs of two letters (digraphs). If there is an odd number
of letters, a Z is added to the last letter. 
Rule 3: write the keyword in 5*5 matrix table.

KEYWORD= MONARCHY

Plaintext = HELLO
CIHPER TEXT=?

1. Pair cannot be made with same letter. Break the letter in single and add a Dummy
letter to the previous letter.

Plain Text: “hello”

After Split: ‘he’ ‘lx’ ‘lo’

Here ‘x’ is the dummy letter.

M O N A R

C H Y B D

E F G I/J K

L P Q S T

U V W X Z

Rules for Encryption: 


RULE 1: If both the letters are in the same row: Take the letter to the right of each one
(going back to the leftmost if at the rightmost position).
RULE 2: If both the letters are in the same column: Take the letter below each one
(going back to the top if at the bottom).
RULE 3: If neither of the above rules is true: Form a rectangle with the two letters and
take the letters on the horizontal opposite corner of the rectangle.
RULE 4: If a pair of letters (digraph) appears in a different row and different column
In this case, select a 3*3 matrix from a 5*5 matrix such that pair of letters appear in the 3*3
matrix. And 2*2 matrix ,Since they occupy two opposite corners of a square within the
matrix. The other corner will be a cipher for the given digraph.
Now to find the cipher for HE, we will consider the opposite to HE, i.e. CF. Therefore,
the cipher for H will be C, and the cipher for E will be F .
CIHPER TEXT text = H E

E F

Now to find the cipher for LX, we will consider the opposite to LX, i.e. US. Therefore,
the cipher for L will be S, and the cipher for X will be U .
Cipher Text = L X

S U

Now to find the cipher for LO, we will consider the opposite to LO,
i.e. MP. Therefore, the cipher for L will be P, and the cipher for O will be M .

Cipher Text = L O

P M
Transposition Cipher
Transposition Cipher is a cryptographic algorithm where the order of alphabets in the
plaintext is rearranged to form a cipher text. In this process, the actual plain text alphabets are
not included.
Example
A simple example for a transposition cipher is columnar transposition cipher where each
character in the plain text is written horizontally with specified alphabet width. The cipher is
written vertically, which creates an entirely different cipher text.
Consider the plain text hello world, and let us apply the simple columnar transposition
technique as shown below

The plain text characters are placed horizontally and the cipher text is created with vertical
format as : holewdlo lr. Now, the receiver has to use the same table to decrypt the cipher text
to plain text.

Rail Fence Cipher – Encryption and Decryption


Given a plain-text message and a numeric key, cipher/de-cipher the given text using Rail
Fence algorithm. 

The rail fence cipher (also called a zigzag cipher) is a form of transposition cipher. It
derives its name from the way in which it is encoded. 

Examples: 
Encryption
Input : "GeeksforGeeks "
Key = 3
Output : GsGsekfrek eoe
Decryption
Input : GsGsekfrek eoe
Key = 3
Output : "GeeksforGeeks "
Encryption
In a transposition cipher, the order of the alphabets is re-arranged to obtain the cipher-text.  
 
 In the rail fence cipher, the plain-text is written downwards and diagonally on
successive rails of an imaginary fence.
 When we reach the bottom rail, we traverse upwards moving diagonally, after
reaching the top rail, the direction is changed again. Thus the alphabets of the
message are written in a zig-zag manner.
 After each alphabet has been written, the individual rows are combined to obtain
the cipher-text.
For example, if the message is “GeeksforGeeks” and the number of rails = 3 then cipher is
prepared as: 
 

Implementation: 
Let cipher-text = “GsGsekfrek eoe” , and Key = 3 

You might also like