Professional Documents
Culture Documents
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
• Refer to the detailed figure for encryption and decryption from the lecture notes.
• The reference figures are shown below for encryption and key generation:
Matlab Code:
• Where parm1,parm2,… are the returned values stored in these variables. The file name and
the name of the function should be same.
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)))
% Encryption
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
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
K1 =
K2 =
MAKING FUNCTION:
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
% 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
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)))
% 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
CT_L =
0 0 1 0
CT_R =
0 0 1 0