Network Security - Programs: Workshop On Communication Networks 6 - 7 January 2017
Network Security - Programs: Workshop On Communication Networks 6 - 7 January 2017
Communication Networks
th th
6 – 7 January 2017
Network Security –
Programs
Prepared by
Mr.G.Balamurugan
Assistant Professor,
Electronics Department, MIT Campus, Anna University
Substitution Cipher: In substitution cipher replaces one symbol with another. If the symbols
in the plaintext are alphabetic characters then replace one character with another. If the
symbols are digits we can replace with another digit.
(a) Mono-alphabetic cipher: A character in the plain text is always changed to same
character in the cipher text regardless of its position in the text. Ex: Letter A is
changed to letter D, every letter A is changed to letter D. Relationship between letters
in the plain text and the cipher text is one to one.
Aim: To ensure security by converting the given text into encrypted code using Mono-
alphabetic substitution cipher.
Formulae:
Encryption algorithm is “shift key characters down”
C=(M+K) mod 26
%% Encryption
plainASCII=abs(plaintext); % To convert to ASCII values
alphapos=mod(plainASCII,65); % To find the position of the character
cipher=alphapos+key;
ciphertext=mod(cipher,26)+65; % Character position to ASCII value
sprintf('Encrypted text : %s',char(ciphertext))
%% Decryption
decrypos=mod(ciphertext,65);
decrypt=decrypos-key;
decrypttext=mod(decrypt,26)+65;
sprintf('Decrypted text : %s',char(decrypttext))
(b) Poly-alphabetic cipher: Each occurrence of a character can have a different substitute.
The relationship between a character in the plaintext to a character in the cipher text is one to
many. For example A could be changed to letter C in the beginning of the text, but as N at the
middle.
Advantage: Poly-alphabetic cipher is hiding the letter of the underlying language. Eve cannot
use single letter frequency statistic to break the cipher text.
Aim: To ensure security by converting the given text into encrypted code using Auto-key
cipher (poly-alphabetic Substitution).
Formulae:
Plaintext (P)=[ p1 p2 p3 p4 p5..... pm ]
Ciphertext (C)= [ c1 c2 c3 c4 c5..... cm ]
Key (K)= [ k1,p1, p2, p3, p4, p5..... pm-1 ]
Program:
%% ----------- Substitution Cipher (Poly-alphabetic) - Auto key cipher ----
plaintext=input('Enter the message : ','s');
key=[];
key=input('Enter key value:');
%% Decryption algorithm
ciphertextpos=mod(ciphertext,65);
decipher=[];
for i=1:length(ciphertextpos)
dicpher(i)=mod(ciphertextpos(i)-key,26);
key=dicpher(i);
end
dicphertext=char(dicpher+65);
fprintf('Decrypted text : %s\n',dicphertext)
Aim: To ensure security by converting the given text into encrypted code using Hill cipher
(poly-alphabetic Substitution).
Theory:
The plaintext is divided into equal size blocks. The blocks are encrypted one at a time in such
a way that each character in the block contributes to the encryption of other character
characters in the block. For this reason, Hill cipher belongs to a category of ciphers called
block ciphers. In a Hill cipher, the key is a square matrix of size MxM in which M is the size
of the block.
Formulae:
Encryption: C=PK mod 26
%% Encryption Algorithm
plaintextblock=reshape(plaintext,2,[])';
plaintextpos=mod(plaintextblock,65);
ciphervalue=mod(plaintextpos*key,26);
cipher=reshape(ciphervalue',1,[]);
ciphertext=char(cipher+65);
fprintf('Encrypted text : %s\n',ciphertext)
%% Decryption Algorithm
receiver=reshape(ciphertext,2,[])';
receivertextpos=mod(receiver,65);
decipherpos=mod(receivertextpos*inversekey,26);
decipher=char(decipherpos+65);
deciphertext=reshape(decipher',1,[]);
fprintf('Decrypted text : %s\n',deciphertext);
Transposition Cipher: It does not substitute one symbol for another; instead it changes the
location of the symbols. A symbol in the first position of the plaintext may appear in the tenth
position of the cipher. In other words, a transposition cipher reorders the symbols.
Keyless Transposition cipher: The text is written into a table row by row and then transmitted
column by column.
For both cipher first divide the plaintext into groups of predetermined size called blocks and
then use a key to permute the characters in each block separately.
Aim: To encrypt and decrypt the given message by using keyless columnar transposition
cipher.
Formulae:
Block Grid size 5
Keyless Transposition cipher: The text is written into a table row by row and then transmitted
column by column.
Program:
clear all
clc
%% Decryption algorithm
Decipher_txt=[];
cipher_txtBlock =reshape(cipher_txt,5,[])';
for i=1:1:length(cipher_txt)/NN
ff=cipher_txtBlock(((i*N)-(N-1)):i*N,1:N);
Decipher_txt=[Decipher_txt reshape(ff,1,[])]; % reading character column by
column
end
fprintf('Decrypted text : %s\n',Decipher_txt)
Aim: To encrypt and decrypt the given message by using keyed columnar transposition
cipher.
Formulae:
Key= [ 2 1 5 3 4]
Keyed Transposition cipher: The keyed ciphers permute the characters by writing the
plaintext in one way (row by row) and reading it in another way (column by column) by
using key. The permutation is done on the whole plain text to create the whole cipher text.
Program:
clear all
clc
%% Encryption Block
cipher_txt=[];
PlaintextBlock =reshape(plain_txt,5,[])';
for i=1:1:length(plain_txt)/NN
ff=PlaintextBlock(((i*N)-(N-1)):i*N,1:N);
ffwithkey=ff(1:5,[2, 1 ,5 ,3, 4])
cipher_txt=[cipher_txt reshape(ffwithkey,1,[])]; % reading character
column by column
end
fprintf('Encrypted text : %s\n',cipher_txt)
%% Decryption algorithm
Decipher_txt=[];
cipher_txtBlock =reshape(cipher_txt,5,[]);
for i=1:1:length(cipher_txt)/NN
ffcipher=cipher_txtBlock(1:N,((i*N)-(N-1)):i*N);
ffcipherwithkey=ffcipher(1:5,[2 1 4 5 3]);
Decipher_txt=[Decipher_txt reshape(ffcipherwithkey',1,[])]; % reading
character column by column
end
fprintf('Decrypted text : %s\n',Decipher_txt)
Theory:
The Advanced Encryption Standard (AES) is symmetric key algorithm, meaning same key is
used for both encrypting and decrypting the data. AES is based on a design principle of
known as substitution and permutation. AES has a fixed block size of 128bits and a key size
of 128, 192 or 256 bits. Encryption consists of 10 rounds of processing for 128-bit keys, 12
rounds for 192-bit keys, and 14 rounds for 256-bit keys. Except for the last round in each
case, all other rounds are identical.
STEP 1: In sub Bytes, each byte in the state is replaced by 8-bit look-up table called
substitution box (S-Box).
STEP 2: In shift row, the bytes in each row of the state are shifted cyclically to the left. The
number of places each byte is shifted differs for each row.
1St Row : Row is left unchanged
2nd Row: Each byte of the row is shifted one to the left
3rd Row: Each byte of the row is shifted twice to the left
4th Row: Each byte of the row is shifted thrice to the left
After shifting, a conditional XOR with 0x1B should be performed if the shifted value is
larger than 0xFF.
STEP 4: In Add Round key, each byte of the state is combined with a byte of the round sub
key using XOR operation. For each round, a sub key is derived from the main key using
Rijndael’s key schedule.
Key Generation:
Program:
ssi;
plaintext=['A' 'B' 'C' 'D' 'E' 'F' 'G' 'H' 'I' 'J' 'K' 'L' 'M' 'N' 'O'
'P'];
plain=reshape(plaintext,4,4);
plain = abs (plain) % Converts to ASCII value
key = ['1' '2' '3' '4' '5' '6' '7' '8' '9' '1' '2' '3' '4' '5' '6' '7'];
key=abs(key);
w = key_expansion (key, s_box, rcon);
% Encryption
ciphertext = cipher (plain, w, s_box, poly_mat,ind_matleft);
ciphertext = reshape(ciphertext,1,16);
% Decryption
deciphertext = reshape (double(ciphertext), 1, 16);
deciphertext =reshape(deciphertext,4,4);
deciphertext = decipher (deciphertext, w, inv_s_box,
inv_poly_mat,ind_matright)
s_box = [
99, 124, 119, 123, 242, 107, 111, 197, 48, 1, 103, 43, 254, 215, 171,
118, ...
202, 130, 201, 125, 250, 89, 71, 240, 173, 212, 162, 175, 156, 164, 114,
192, ...
183, 253, 147, 38, 54, 63, 247, 204, 52, 165, 229, 241, 113, 216, 49,
21, ...
4, 199, 35, 195, 24, 150, 5, 154, 7, 18, 128, 226, 235, 39, 178,
117, ...
9, 131, 44, 26, 27, 110, 90, 160, 82, 59, 214, 179, 41, 227, 47,
132, ...
83, 209, 0, 237, 32, 252, 177, 91, 106, 203, 190, 57, 74, 76, 88,
207, ...
208, 239, 170, 251, 67, 77, 51, 133, 69, 249, 2, 127, 80, 60, 159,
168, ...
81, 163, 64, 143, 146, 157, 56, 245, 188, 182, 218, 33, 16, 255, 243,
210, ...
205, 12, 19, 236, 95, 151, 68, 23, 196, 167, 126, 61, 100, 93, 25,
115, ...
96, 129, 79, 220, 34, 42, 144, 136, 70, 238, 184, 20, 222, 94, 11,
219, ...
224, 50, 58, 10, 73, 6, 36, 92, 194, 211, 172, 98, 145, 149, 228,
121, ...
231, 200, 55, 109, 141, 213, 78, 169, 108, 86, 244, 234, 101, 122, 174,
8, ...
186, 120, 37, 46, 28, 166, 180, 198, 232, 221, 116, 31, 75, 189, 139,
138, ...
inv_s_box = [
82, 9, 106, 213, 48, 54, 165, 56, 191, 64, 163, 158, 129, 243, 215,
251, ...
124, 227, 57, 130, 155, 47, 255, 135, 52, 142, 67, 68, 196, 222, 233,
203, ...
84, 123, 148, 50, 166, 194, 35, 61, 238, 76, 149, 11, 66, 250, 195,
78, ...
8, 46, 161, 102, 40, 217, 36, 178, 118, 91, 162, 73, 109, 139, 209,
37, ...
114, 248, 246, 100, 134, 104, 152, 22, 212, 164, 92, 204, 93, 101, 182,
146, ...
108, 112, 72, 80, 253, 237, 185, 218, 94, 21, 70, 87, 167, 141, 157,
132, ...
144, 216, 171, 0, 140, 188, 211, 10, 247, 228, 88, 5, 184, 179, 69,
6, ...
208, 44, 30, 143, 202, 63, 15, 2, 193, 175, 189, 3, 1, 19, 138,
107, ...
58, 145, 17, 65, 79, 103, 220, 234, 151, 242, 207, 206, 240, 180, 230,
115, ...
150, 172, 116, 34, 231, 173, 53, 133, 226, 249, 55, 232, 28, 117, 223,
110, ...
71, 241, 26, 113, 29, 41, 197, 137, 111, 183, 98, 14, 170, 24, 190,
27, ...
252, 86, 62, 75, 198, 210, 121, 32, 154, 219, 192, 254, 120, 205, 90,
244, ...
31, 221, 168, 51, 136, 7, 199, 49, 177, 18, 16, 89, 39, 128, 236,
95, ...
96, 81, 127, 169, 25, 181, 74, 13, 45, 229, 122, 159, 147, 201, 156,
239, ...
160, 224, 59, 77, 174, 42, 245, 176, 200, 235, 187, 60, 131, 83, 153,
97, ...
23, 43, 4, 126, 186, 119, 214, 38, 225, 105, 20, 99, 85, 33, 12,
125];
inv_poly_mat = [
14,11, 13, 9,
9, 14, 11, 13,
13, 9, 14, 11,
11, 13, 9, 14];
poly_mat = [2,3,1,1,
1,2,3,1,
1,1,2,3,
3,1,1,2];
rcon=[ 1 0 0 0
2 0 0 0
4 0 0 0
8 0 0 0
16 0 0 0
32 0 0 0
ind_matleft =[
1 5 9 13
6 10 14 2
11 15 3 7
16 4 8 12];
ind_matright =[1 5 9 13
14 2 6 10
11 15 3 7
8 12 16 4];
Program:
clc;
clear all;
close all;
%% Encryption Algorithm
k=1;
g=0;
while(g<e)
g=g+1;
k=mod(m.*k,n)
end
c=k;
disp('the ascii value of the encrypted message is as follows: ');
disp(c);
cipher=char(c);
disp('the encrypted message is as follows: ');
disp(cipher);
%% Decryption Algorithm
k=1;
g=0;
while(g<d)
g=g+1;
k=mod(c.*k,n);
end
deascii=k;
disp('the ascii value of the decrypted message is as follows: ');
disp(deascii);
desrypt_message=char(deascii);
disp('the decrypted message is as follows: ');
disp(desrypt_message);
%%
if(isempty(d))
disp('the inverse does not exist for the data!!!!!!');
end