You are on page 1of 12

CE-408: Cryptography & Network Security SSUET/QR/114

LAB#04
Modern Encryption Techniques
Simplified DES (S-DES)
OBJECTIVE

The purpose of this lab is to implement SDES encryption and decryption in MATLAB.

THEORY
• Encryption: It takes an 8-bit block of plain text and a 10-bit key as input and produces an
8-bit block of cipher text as output.
• Decryption: It takes an 8-bit block of cipher text and the same 10-bit key used to produce
that ciphertext as input and produces the original 8-bit block of plaintext.
• Algorithm involves 5 functions:
1. An initial permutation (IP).
2. A complex function, fK , that involves both permutation and substitution operations and
depends on the sub key input. In the first fK block, sub key 1 is used.
3. A simple permutation function that switches the two halves of the data (SW).
4. The function fK again with sub key 2 being used in this case.
5. A permutation function that is the inverse of the initial one (IP-1) as shown below

C = (IP -1
fK 2
SW fK 1
IP)
or

Ciphertext = IP -1(fK (SW(f (IP(plaint ext)))))


K1
where 2

K1 = P8(Left Shift - 1 (P10(key)) )

K 2 = P8(Left Shift - 2(Left Shift - 1(P10(key) )))


and

Plaintext = IP -1(fK 1 (SW(f K2 (IP(cipher text)))))


Lab#04: Simplified DES (SDES) Page | 1
CE-408: Cryptography & Network Security SSUET/QR/114

• Refer to the detailed figure for encryption and decryption from the lecture notes.
• The reference figures are shown below for encryption and key generation:

Lab#04: Simplified DES (SDES) Page | 2


CE-408: Cryptography & Network Security SSUET/QR/114

Matlab Code:

• The constants being used are hard coded first in MATLAB.


• Create three functions, main, key generator and fk function. The key and fk functions are
going to be called from the main body.
• Define the constants within the main body and pass them to the appropriate functions.
• The format for basic function is as follows:

function [parm1,parm2,…] = Function_name(passing_parm1, passing_parm2,…)

• Where parm1,parm2,… are the returned values stored in these variables. The file name and
the name of the function should be same.

Lab#04: Simplified DES (SDES) Page | 3


CE-408: Cryptography & Network Security SSUET/QR/114

Lab Tasks:
1. Write the program for the main code for encryption through which the key and fk function
are called. Perform Initial permutation, switching and inverse permutation here. Display the
final ciphertext along with key and plaintext.
CODE:
clear all
clc

% Permutation Formulae
P10 = [ 3 5 2 7 4 10 1 9 8 6];
P8 = [ 6 3 7 4 8 5 10 9];
P4 = [ 2 4 3 1];
EP = [ 4 1 2 3 2 3 4 1];
IP = [ 2 6 3 1 4 8 5 7];
IP_INV = [ 4 1 3 5 7 2 8 6];
S0 = [ 1 0 3 2;
3 2 1 0;
0 2 1 3;
3 1 3 2 ];
S1 = [ 0 1 2 3;
2 0 1 3;
3 0 1 0;
2 1 0 3 ];

% Plain Text
PT = [ 0 0 1 0 1 0 0 0];
disp(strcat('Plain Text :', int2str(PT)))

%S-DES Key Gen


% Input Key
KEY = [ 1 1 0 0 0 1 1 1 1 0];
disp(strcat('Input Key :', int2str(KEY)))

% P10(Key) Applying P10 permutation on Key


KEY_P10 = KEY(P10);
% Separating Left and Right from P10(Key)
L = KEY_P10(1:5);
R = KEY_P10(6:10);
% Performing Circular Left Shift on both L and R
L = circshift(L, [1, -1]);
R = circshift(R, [1, -1]);
% Combining L and R
K1 = [L R];
% P8(K) Applying P8 permutation on K
K1 = K1(P8); % K1 Ans Generated
disp(strcat('K1 Output Key :', int2str(K1)))
% Applying double circular left shift on L and R separately
L = circshift(circshift(L, [1, -1]), [1, -1]);
R = circshift(circshift(R, [1, -1]), [1, -1]);
% Combining L and R
K2 = [L R];
% P8(K) Applying P8 permutation on K
K2 = K2(P8); % K2 Ans Generated
disp(strcat('K2 Output Key :', int2str(K2)))
Lab#04: Simplified DES (SDES) Page | 4
CE-408: Cryptography & Network Security SSUET/QR/114

% Encryption

% Apply IP permutation on Plain Text


PT_IP = PT(IP);
% Separate IP(Plain_Text) into L and R
PT_L = PT_IP(1:4);
PT_R = PT_IP(5:8);
% fk1(L xor F(R, K1), R)
% Perform F(R, K1)
R_EP = PT_R(EP); % EP permutation on R
PT_K1 = bitxor(R_EP, K1); % XORing EP(R) with K1
PT_K1_L = PT_K1(1:4); % Separating L
PT_K1_R = PT_K1(5:8); % Separating R
% SBoxes Part L
RR = strcat(int2str(PT_K1_L(1)), int2str(PT_K1_L(4))); % RR Combination from L
S0_ROW = bin2dec(RR)+1; % Bin Index to Dec Index conversion
CC = strcat(int2str(PT_K1_L(2)), int2str(PT_K1_L(3))); % CC Combination from L
S0_COL = bin2dec(CC)+1; % Bin Index to Dec Index conversion
S0_K1 = dec2bin(S0(S0_ROW, S0_COL),2) - '0'; % finding 2 bit digit from S0 using
RR and CC
% SBoxes Part R
RR = strcat(int2str(PT_K1_R(1)), int2str(PT_K1_R(4))); % RR for R
S1_ROW = bin2dec(RR)+1;
CC = strcat(int2str(PT_K1_R(2)), int2str(PT_K1_R(3))); % CC for R
S1_COL = bin2dec(CC)+1;
S1_K1 = dec2bin(S1(S1_ROW, S1_COL),2) - '0';
% Finalizing F(R, SK)
SB = [S0_K1 S1_K1];
PT_P4 = SB(P4);
% L xor F(R, SK)
PT_L = bitxor(PT_L, PT_P4);
% SW Switch
Temp = PT_L;
PT_L = PT_R;
PT_R = Temp;
% fk2(L xor F(R, K2), R)
% Applying F(R, K2)
PT_EP = PT_R(EP);
PT_K2 = bitxor(PT_EP, K2);
PT_K2_L = PT_K2(1:4);
PT_K2_R = PT_K2(5:8);

RR = strcat(int2str(PT_K2_L(1)), int2str(PT_K2_L(4)));
S0_ROW = bin2dec(RR)+1;
CC = strcat(int2str(PT_K2_L(2)), int2str(PT_K2_L(3)));
S0_COL = bin2dec(CC)+1;
S0_K2 = dec2bin(S0(S0_ROW, S0_COL),2) - '0';

RR = strcat(int2str(PT_K2_R(1)), int2str(PT_K2_R(4)));
S1_ROW = bin2dec(RR)+1;
CC = strcat(int2str(PT_K2_R(2)), int2str(PT_K2_R(3)));
S1_COL = bin2dec(CC)+1;
S1_K2 = dec2bin(S1(S1_ROW, S1_COL),2) - '0';

SB = [S0_K2 S1_K2];
PT_P4 = SB(P4);
PT_L = bitxor(PT_L, PT_P4);
Lab#04: Simplified DES (SDES) Page | 5
CE-408: Cryptography & Network Security SSUET/QR/114
% Combining L and R
PT_Cipher = [PT_L, PT_R];
% Applying IP-1 permutation
PT_C_IP_INV = PT_Cipher(IP_INV);
disp(strjoin({'Encrypted Plain Text :',int2str(PT_C_IP_INV)},' '))

% Decryption
CT = PT_C_IP_INV;
% Apply IP permutation on Plain Text
CT_IP = CT(IP);
% Separate IP(Plain_Text) into L and R
CT_L = CT_IP(1:4);
CT_R = CT_IP(5:8);
% fk2(L xor F(R, K2), R)
% Applying F(R, K2)
CT_EP = CT_R(EP);
CT_K2 = bitxor(CT_EP, K2);
CT_K2_L = CT_K2(1:4);
CT_K2_R = CT_K2(5:8);

RR = strcat(int2str(CT_K2_L(1)), int2str(CT_K2_L(4)));
S0_ROW = bin2dec(RR)+1;
CC = strcat(int2str(CT_K2_L(2)), int2str(CT_K2_L(3)));
S0_COL = bin2dec(CC)+1;
S0_K2 = dec2bin(S0(S0_ROW, S0_COL),2) - '0';

RR = strcat(int2str(CT_K2_R(1)), int2str(CT_K2_R(4)));
S1_ROW = bin2dec(RR)+1;
CC = strcat(int2str(CT_K2_R(2)), int2str(CT_K2_R(3)));
S1_COL = bin2dec(CC)+1;
S1_K2 = dec2bin(S1(S1_ROW, S1_COL),2) - '0';

SB = [S0_K2 S1_K2];
CT_P4 = SB(P4);
CT_L = bitxor(CT_L, CT_P4);
% SW Switch
Temp = CT_L;
CT_L = CT_R;
CT_R = Temp;
% fk1(L xor F(R, K1), R)
% Perform F(R, K1)
R_EP = CT_R(EP); % EP permutation on R
CT_K1 = bitxor(R_EP, K1); % XORing EP(R) with K1
CT_K1_L = CT_K1(1:4); % Separating L
CT_K1_R = CT_K1(5:8); % Separating R
% SBoxes Part L
RR = strcat(int2str(CT_K1_L(1)), int2str(CT_K1_L(4))); % RR Combination from L
S0_ROW = bin2dec(RR)+1; % Bin Index to Dec Index conversion
CC = strcat(int2str(CT_K1_L(2)), int2str(CT_K1_L(3))); % CC Combination from L
S0_COL = bin2dec(CC)+1; % Bin Index to Dec Index conversion
S0_K1 = dec2bin(S0(S0_ROW, S0_COL),2) - '0'; % finding 2 bit digit from S0 using
RR and CC
% SBoxes Part R
RR = strcat(int2str(CT_K1_R(1)), int2str(CT_K1_R(4))); % RR for R
S1_ROW = bin2dec(RR)+1;
CC = strcat(int2str(CT_K1_R(2)), int2str(CT_K1_R(3))); % CC for R
S1_COL = bin2dec(CC)+1;
S1_K1 = dec2bin(S1(S1_ROW, S1_COL),2) - '0';
Lab#04: Simplified DES (SDES) Page | 6
CE-408: Cryptography & Network Security SSUET/QR/114

%Finalizing F(R, SK)


SB = [S0_K1 S1_K1];
CT_P4 = SB(P4);
% L xor F(R, SK)
CT_L = bitxor(CT_L, CT_P4);
% Combining L and R
CT_Decrypted = [CT_L, CT_R];
% Applying IP-1 permutation
CT_C_IP_INV = CT_Decrypted(IP_INV);
disp(strjoin({'Decrypted Cipher Text :',int2str(CT_C_IP_INV)},' '))

OUTPUT:

Plain Text :0 0 1 0 1
Input Key :1 1 0 0 0 1 1 0
K1 Output Key :1 1 1 0 0 1
K2 Output Key :1 0 1 0 1 1
Encrypted Plain Text : 1 0 1 0 1 0
Decrypted Cipher Text : 0 0 1 0

Lab#04: Simplified DES (SDES) Page | 7


CE-408: Cryptography & Network Security SSUET/QR/114

2. Write the program for sub key generation.


CODE:

% Write the program for sub key generation.


function [ K1, K2 ] = KGen( KEY, P10, P8 )
disp(strcat('Input Key :', int2str(KEY)))
% P10(Key) Applying P10 permutation on Key
KEY_P10 = KEY(P10);
% Separating Left and Right from P10(Key)
L = KEY_P10(1:5);
R = KEY_P10(6:10);
% Performing Circular Left Shift on both L and R
L = circshift(L, [1, -1]);
R = circshift(R, [1, -1]);
% Combining L and R
K1 = [L R];
% P8(K) Applying P8 permutation on K
K1 = K1(P8); % K1 Ans Generated
disp(strcat('K1 Output Key :', int2str(K1)))
% Applying double circular left shift on L and R separately
L = circshift(circshift(L, [1, -1]), [1, -1]);
R = circshift(circshift(R, [1, -1]), [1, -1]);
% Combining L and R
K2 = [L R];
% P8(K) Applying P8 permutation on K
K2 = K2(P8); % K2 Ans Generated
disp(strcat('K2 Output Key :', int2str(K2)))
end
OUTPUT:
>> clear all
>> P10 = [ 3 5 2 7 4 10 1 9 8 6];
>> P8 = [ 6 3 7 4 8 5 10 9];
>> KEY = [ 1 1 0 0 0 1 1 1 1 0];
>> [K1, K2] = KGen(KEY, P10, P8)
Input Key :1 1 0 0 0 1 1 1 1 0
K1 Output Key :1 1 1 0 1 0 0 1
K2 Output Key :1 0 1 0 0 1 1 1

K1 =

K2 =

Lab#04: Simplified DES (SDES) Page | 8


CE-408: Cryptography & Network Security SSUET/QR/114

3. Write the program for the fk function.


CODE:

MAKING FUNCTION:

% Write the program for the fk function.


function [PT_L, PT_R] = fK(PT_L, PT_R, SK, EP, P4, S0, S1)
% fk1(L xor F(R, K1), R)
% Perform F(R, K1)
R_EP = PT_R(EP); % EP permutation on R
PT_K1 = bitxor(R_EP, SK); % XORing EP(R) with SK
PT_K1_L = PT_K1(1:4); % Separating L
PT_K1_R = PT_K1(5:8); % Separating R
% SBoxes Part L
RR = strcat(int2str(PT_K1_L(1)), int2str(PT_K1_L(4))); % RR CombinationfromL
S0_ROW = bin2dec(RR)+1; % Bin Index to Dec Index conversion
CC = strcat(int2str(PT_K1_L(2)), int2str(PT_K1_L(3))); % CC CombinationfromL
S0_COL = bin2dec(CC)+1; % Bin Index to Dec Index conversion
S0_K1 = dec2bin(S0(S0_ROW, S0_COL),2) - '0'; % finding 2 bit digit from S0 using RR
and CC
% SBoxes Part R
RR = strcat(int2str(PT_K1_R(1)), int2str(PT_K1_R(4))); % RR for R
S1_ROW = bin2dec(RR)+1;
CC = strcat(int2str(PT_K1_R(2)), int2str(PT_K1_R(3))); % CC for R
S1_COL = bin2dec(CC)+1;
S1_K1 = dec2bin(S1(S1_ROW, S1_COL),2) - '0';
% Finalizing F(R, SK)
SB = [S0_K1 S1_K1];
PT_P4 = SB(P4);
% L xor F(R, SK)
PT_L = bitxor(PT_L, PT_P4);
end

CALLING FUNCTION:

clear all
clc
% Permutation Formulae
P10 = [ 3 5 2 7 4 10 1 9 8 6];
P8 = [ 6 3 7 4 8 5 10 9];
P4 = [ 2 4 3 1];
EP = [ 4 1 2 3 2 3 4 1];
IP = [ 2 6 3 1 4 8 5 7];
S0 = [ 1 0 3 2;
3 2 1 0;
0 2 1 3;
3 1 3 2 ];
S1 = [ 0 1 2 3;
2 0 1 3;
3 0 1 0;
2 1 0 3 ];
% Plain Text

PT = [ 0 0 1 0 1 0 0 0];
Lab#04: Simplified DES (SDES) Page | 9
CE-408: Cryptography & Network Security SSUET/QR/114

%S-DES Key Gen

% Input Key

KEY = [ 1 1 0 0 0 1 1 1 1 0];
[K1, K2] = KGen(KEY, P10,P8);
% Apply IP permutation on Plain Text
PT_IP = PT(IP);
% Separate IP(Plain_Text) into L and R
PT_L = PT_IP(1:4);
PT_R = PT_IP(5:8);
% fk1(L xor F(R, K1), R)
[PT_L, PT_R] = fK(PT_L, PT_R, K1, EP, P4, S0, S1)

OUTPUT:

Input Key :1 1 0 0 0 1 1 1 1 0
K1 Output Key : 1 1 1 0 1 0 0 1
K2 Output Key : 1 0 1 0 0 1 1 1

PT_L =

0 0 1 1

PT_R =

0 0 1 0

Lab#04: Simplified DES (SDES) Page | 10


CE-408: Cryptography & Network Security SSUET/QR/114

4. Write the program for the main code for decryption through which the key and fk function
are called. Perform Initial permutation, switching and inverse permutation here. Display the
final plaintext along with key and plaintext.
CODE:
% Write the program for the main code for decryption through which the key
% and fk function are called. Perform Initial permutation, switching and
% inverse permutation here. Display the final plaintext along with key and
plaintext.
clear all
clc

% Permutation Formulae
P10 = [ 3 5 2 7 4 10 1 9 8 6];
P8 = [ 6 3 7 4 8 5 10 9];
P4 = [ 2 4 3 1];
EP = [ 4 1 2 3 2 3 4 1];
IP = [ 2 6 3 1 4 8 5 7];
IP_INV = [ 4 1 3 5 7 2 8 6];
S0 = [ 1 0 3 2;
3 2 1 0;
0 2 1 3;
3 1 3 2 ];
S1 = [ 0 1 2 3;
2 0 1 3;
3 0 1 0;
2 1 0 3 ];

% Plain Text
PT = [ 0 0 1 0 1 0 0 0];
disp(strcat('Plain Text :', int2str(PT)))

%S-DES Key Gen


% Input Key
KEY = [ 1 1 0 0 0 1 1 1 1 0];
[K1, K2] = KGen(KEY, P10, P8)
% Encryption

% Apply IP permutation on Plain Text


PT_IP = PT(IP);
% Separate IP(Plain_Text) into L and R
PT_L = PT_IP(1:4);
PT_R = PT_IP(5:8);
% fk1(L xor F(R, K1), R)
[PT_L, PT_R] = fK(PT_L, PT_R, K1, EP, P4, S0, S1);
% SW Switch
Temp = PT_L;
PT_L = PT_R;
PT_R = Temp;
% fk2(L xor F(R, K2), R)
[PT_L, PT_R] = fK(PT_L, PT_R, K2, EP, P4, S0, S1)
PT_Cipher = [PT_L, PT_R];
% Applying IP-1 permutation
PT_C_IP_INV = PT_Cipher(IP_INV);
disp(strjoin({'Encrypted Plain Text :',int2str(PT_C_IP_INV)},' '))

% Decryption
Lab#04: Simplified DES (SDES) Page | 11
CE-408: Cryptography & Network Security SSUET/QR/114
CT = PT_C_IP_INV;
% Apply IP permutation on Plain Text
CT_IP = CT(IP);
% Separate IP(Plain_Text) into L and R
CT_L = CT_IP(1:4);
CT_R = CT_IP(5:8);
% fk2(L xor F(R, K2), R)
[CT_L, CT_R] = fK(CT_L, CT_R, K2, EP, P4, S0, S1);
% SW Switch
Temp = CT_L;
CT_L = CT_R;
CT_R = Temp;
% fk1(L xor F(R, K1), R)
[CT_L, CT_R] = fK(CT_L, CT_R, K1, EP, P4, S0, S1)
CT_Decrypted = [CT_L, CT_R];
% Applying IP-1 permutation
CT_C_IP_INV = CT_Decrypted(IP_INV);
disp(strjoin({'Decrypted Cipher Text :',int2str(CT_C_IP_INV)},' '))
OUTPUT:
Plain Text :0 0 1 0 1 0 0 0
Input Key :1 1 0 0 0 1 1 1 1 0
K1 Output Key :1 1 1 0 1 0 0 1
K2 Output Key :1 0 1 0 0 1 1 1

K1 =

1 1 1 0 1 0 0 1

K2 =

1 0 1 0 0 1 1 1

PT_L =

0 0 0 1

PT_R =

0 0 1 1

Encrypted Plain Text : 1 0 0 0 1 0 1 0

CT_L =

0 0 1 0

CT_R =

0 0 1 0

Decrypted Cipher Text : 0 0 1 0 1 0 0 0

Lab#04: Simplified DES (SDES) Page | 12

You might also like