0% found this document useful (0 votes)
4 views4 pages

Crypto Lab 4

The document outlines a laboratory exercise focused on implementing Simplified DES (S-DES) encryption and decryption using MATLAB. It includes tasks for writing programs for encryption, subkey generation, the fk function, and decryption, detailing the necessary steps and code snippets for each task. The objective is to demonstrate the workings of modern encryption techniques through practical coding exercises.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Crypto Lab 4

The document outlines a laboratory exercise focused on implementing Simplified DES (S-DES) encryption and decryption using MATLAB. It includes tasks for writing programs for encryption, subkey generation, the fk function, and decryption, detailing the necessary steps and code snippets for each task. The objective is to demonstrate the workings of modern encryption techniques through practical coding exercises.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Cryptography & Network Security LAB#4 2021F-BCE-029

LAB#04
MODERN ENCRYPTION TECHNIQUES
SIMPLIFIED DES (S-DES)
OBJECTIVE To implement SDES encryption and decryption in MATLAB.
Task1:
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:
plaintext = [1 0 1 0 0 0 1 1 row = bin2dec([num2str(xor1(1))
key = [1 0 1 0 0 0 0 0 1 num2str(xor1(4))]) + 1;
1fprintf('Original Plaintext: '); col = bin2dec([num2str(xor1(2))
disp(plaintext); num2str(xor1(3))]) + 1;
val = S0(row, col);
fprintf('10-bit Key: '); s0 = decimalTo2BitBinary(val);
disp(key);
% S-Box 1
%% Step 1: Key Generation row = bin2dec([num2str(xor1(5))
P10 = [3 5 2 7 4 10 1 9 8 6]; num2str(xor1(8))]) + 1;
P8 = [6 3 7 4 8 5 10 9]; col = bin2dec([num2str(xor1(6))
num2str(xor1(7))]) + 1;
keyP10 = key(P10); val = S1(row, col);
left = keyP10(1:5); s1 = decimalTo2BitBinary(val);
right = keyP10(6:10);
s_output = [s0 s1];
% Left shift-1 s_permuted = s_output(P4);
left1 = circshift(left, -1); fk1 = xor(L, s_permuted);
right1 = circshift(right, -1);
k1 = [left1 right1]; round1 = [fk1 R];
k1 = k1(P8);
%% Step 4: Switch
% Left shift-2 switched = [round1(5:8) round1(1:4)];
left2 = circshift(left1, -2);
right2 = circshift(right1, -2); %% Step 5: Second round (fk with K2)
k2 = [left2 right2]; L2 = switched(1:4);
k2 = k2(P8); R2 = switched(5:8);
expandedR2 = R2(EP);
fprintf('Subkey 1 (K1): '); xor2 = xor(expandedR2, k2);
disp(k1);
% S-Box 0
fprintf('Subkey 2 (K2): '); row = bin2dec([num2str(xor2(1))
disp(k2); num2str(xor2(4))]) + 1;
col = bin2dec([num2str(xor2(2))
%% Step 2: Initial Permutation num2str(xor2(3))]) + 1;
IP = [2 6 3 1 4 8 5 7]; val = S0(row, col);
data = plaintext(IP); s0 = decimalTo2BitBinary(val);

%% Step 3: First round (fk with K1) % S-Box 1


EP = [4 1 2 3 2 3 4 1]; row = bin2dec([num2str(xor2(5))
P4 = [2 4 3 1]; num2str(xor2(8))]) + 1;
S0 = [1 0 3 2; 3 2 1 0; 0 2 1 3; 3 1 3 2]; col = bin2dec([num2str(xor2(6))
S1 = [0 1 2 3; 2 0 1 3; 3 0 1 0; 2 1 0 3]; num2str(xor2(7))]) + 1;
val = S1(row, col);
L = data(1:4); s1 = decimalTo2BitBinary(val);
R = data(5:8);
s_output2 = [s0 s1];
expandedR = R(EP); s_permuted2 = s_output2(P4);
xor1 = xor(expandedR, k1); fk2 = xor(L2, s_permuted2);

% S-Box 0 preOutput = [fk2 R2];


Cryptography & Network Security LAB#4 2021F-BCE-029

%% Step 6: Inverse Initial Permutation elseif x == 1


IP_inv = [4 1 3 5 7 2 8 6]; b = [0 1];
ciphertext = preOutput(IP_inv); elseif x == 2
b = [1 0];
fprintf('Final Ciphertext: '); elseif x == 3
disp(ciphertext); b = [1 1];
else
%% Helper function to convert 0–3 to 2-bit error('Invalid input to binary
binary (no toolbox needed) converter');
function b = decimalTo2BitBinary(x) end
if x == 0 end
b = [0 0];

Output:

Task2:
Write the program for sub key generation.
Code
key = [1 0 1 0 0 0 0 0 1 1]; % Example key
% Combine and apply P8 permutation
fprintf('Original 10-bit Key: '); combined1 = [left1 right1];
disp(key); P8 = [6 3 7 4 8 5 10 9];
K1 = combined1(P8);
% Step 1: P10 permutation fprintf('Subkey K1: ');
P10 = [3 5 2 7 4 10 1 9 8 6]; disp(K1);
keyP10 = key(P10);
fprintf('After P10 Permutation: '); % Step 4: Left shift both halves by 2 more
disp(keyP10); positions → LS-2
left2 = circshift(left1, -2);
% Step 2: Split into left and right halves right2 = circshift(right1, -2);
left = keyP10(1:5);
right = keyP10(6:10); % Combine and apply P8 again for K2
combined2 = [left2 right2];
% Step 3: Left shift both halves by 1 → LS-1 K2 = combined2(P8);
left1 = circshift(left, -1); % circular fprintf('Subkey K2: ');
left shift disp(K2);
right1 = circshift(right, -1);

Output:

Task3:
Write the program for the fk function.
Code:
Cryptography & Network Security LAB#4 2021F-BCE-029

data = [1 0 1 1 0 0 1 1]; % example 8-bit


input after initial permutation % S1 input
subkey = [0 1 1 0 1 0 1 1]; % example 8-bit row = bin2dec([num2str(xorResult(5))
subkey (K1 or K2) num2str(xorResult(8))]) + 1;
col = bin2dec([num2str(xorResult(6))
fprintf('Input 8-bit Data: '); num2str(xorResult(7))]) + 1;
disp(data); s1val = S1(row, col);
fprintf('Subkey: '); s1bin = decimalTo2BitBinary(s1val);
disp(subkey);
% Step 5: P4 permutation
% Step 1: Split into Left and Right halves P4 = [2 4 3 1];
L = data(1:4); sOutput = [s0bin s1bin];
R = data(5:8); sPermuted = sOutput(P4);

% Step 2: Expansion/Permutation (EP) % Step 6: XOR with left half


EP = [4 1 2 3 2 3 4 1]; fkOutputLeft = xor(L, sPermuted);
expandedR = R(EP); fkResult = [fkOutputLeft R]; % combine with
unchanged right half
% Step 3: XOR with subkey
xorResult = xor(expandedR, subkey); fprintf('fk Output (Left XORed + Right): ');
disp(fkResult);
% Step 4: S-box substitutions
S0 = [1 0 3 2; %% Helper function (no toolbox used)
3 2 1 0; function b = decimalTo2BitBinary(x)
0 2 1 3; if x == 0
3 1 3 2]; b = [0 0];
S1 = [0 1 2 3; elseif x == 1
2 0 1 3; b = [0 1];
3 0 1 0; elseif x == 2
2 1 0 3]; b = [1 0];
elseif x == 3
% S0 input b = [1 1];
row = bin2dec([num2str(xorResult(1)) else
num2str(xorResult(4))]) + 1; error('Invalid input to binary
col = bin2dec([num2str(xorResult(2)) converter');
num2str(xorResult(3))]) + 1; end
s0val = S0(row, col); end
s0bin = decimalTo2BitBinary(s0val);

Output:

Task4:
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:
% 10-bit Key (change as needed) disp(ciphertext);
key = [1 0 1 0 0 0 0 0 1 1]; % Example key P10 = [3 5 2 7 4 10 1 9 8 6];
P8 = [6 3 7 4 8 5 10 9];
% 8-bit Ciphertext (change as needed)
ciphertext = [0 1 1 0 1 1 1 0]; % Encrypted % P10 permutation
message keyP10 = key(P10);

fprintf('10-bit Key: '); % Splitting


disp(key); L = keyP10(1:5);
fprintf('8-bit Ciphertext: '); R = keyP10(6:10);
Cryptography & Network Security LAB#4 2021F-BCE-029

3 2 1 0;
% LS-1 0 2 1 3;
L1 = circshift(L, -1); 3 1 3 2];
R1 = circshift(R, -1);
S1 = [0 1 2 3;
% K1 2 0 1 3;
K1 = [L1 R1]; 3 0 1 0;
K1 = K1(P8); 2 1 0 3];

% LS-2 % S0 input
L2 = circshift(L1, -2); row = bin2dec([num2str(xorResult(1))
R2 = circshift(R1, -2); num2str(xorResult(4))]) + 1;
col = bin2dec([num2str(xorResult(2))
% K2 num2str(xorResult(3))]) + 1;
K2 = [L2 R2]; s0val = S0(row, col);
K2 = K2(P8); s0 = decimalTo2BitBinary(s0val);

fprintf('Subkey K1: '); % S1 input


disp(K1); row = bin2dec([num2str(xorResult(5))
fprintf('Subkey K2: '); num2str(xorResult(8))]) + 1;
disp(K2); col = bin2dec([num2str(xorResult(6))
IP = [2 6 3 1 4 8 5 7]; num2str(xorResult(7))]) + 1;
IPinv = [4 1 3 5 7 2 8 6]; s1val = S1(row, col);
s1 = decimalTo2BitBinary(s1val);
initial = ciphertext(IP);
fk1 = fk(initial, K2); % P4 permutation
switched = [fk1(5:8) fk1(1:4)]; P4 = [2 4 3 1];
fk2 = fk(switched, K1); sOutput = [s0 s1];
plaintext = fk2(IPinv); sPermuted = sOutput(P4);

fprintf('Decrypted Plaintext:'); % XOR with Left Half


disp(plaintext); Lout = xor(L, sPermuted);
function result = fk(bits, subkey)
% Split into L and R result = [Lout R]; % Combine with
L = bits(1:4); unchanged Right
R = bits(5:8); end
function b = decimalTo2BitBinary(x)
% Expansion/Permutation switch x
EP = [4 1 2 3 2 3 4 1]; case 0, b = [0 0];
expandedR = R(EP); case 1, b = [0 1];
case 2, b = [1 0];
% XOR with subkey case 3, b = [1 1];
xorResult = xor(expandedR, subkey); otherwise, error('Invalid input');
end
% S-boxes end
S0 = [1 0 3 2;

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy