Crypto Lab 4
Crypto Lab 4
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);
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
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);
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);
Output: