Question: Modify the S-DES program written in MATLAB) below to work in triple-S-DES mode. UserChoice = input('Please press 1 for decryption or 2 for encryption:'); if
Modify the S-DES program written in MATLAB) below to work in triple-S-DES mode.
UserChoice = input('Please press 1 for decryption or 2 for encryption:');
if UserChoice == 1;
Ciphertext = input('Enter 8 bits Ciphertext.use solid bracket,space between each bit and dont press enter b4 the 8th bit):');
in = Ciphertext; % 8 bit ciphertext, which is needed to be decrypted
IP = [2 6 3 1 4 8 5 7]; % Initial permutation.
EP = [4 1 2 3 2 3 4 1]; % Expand permutation.
s0 = [1 0 3 2; 3 2 1 0; 0 2 1 3; 3 1 3 2]; % 4 bits to 2 bits changer for s0
s1 = [0 1 2 3; 2 0 1 3; 3 0 1 0; 2 1 0 3]; % 4 bits to 2 bits changer for s1
N = [2 4 3 1]; % P4 permutation
IPI = [4 1 3 5 7 2 8 6]; % Inverse permutation
K = [1 1 1 1 1 1 0 0 0 1 0 1 1 1 1 1]; % K2 and K1 respectively
newK = [1 0 0 1 0 1 1 1 1 1 0 0 1 1 0 ];
%fk one of the mysterious block starts here
for e=1:8
LR0(e) = in(IP(e)); % Initial permutation
end
L0 = LR0(1:4); %left 4 bit
R0 = LR0(5:8); %Right 4 bit
z = 0;
for s = 1: 2
for e=1:8
ER0(e) = R0(EP(e)); %Expansion of right 4 bit
end
a = xor(ER0,K(1+z*2:8+z*2));
u = s0((a(2)*2 + a(3))*4 + (a(1)*2 + a(4)) + 1);
v = s1((a(6)*2 + a(7))*4 + (a(5)*2 + a(8)) + 1);
w = [floor(u/2) rem(u,2) floor(v/2) rem(v,2)]; %to binary%
for e=1:4
b(e) = w(N(e));
end
R1(1+z:4+z) = xor(L0,b);
L0 = R0; %S-Box
R0 = R1; %S-Box
z = z+4; % for 2nd cycle%
end
R2L2 = [R1(5:8) R1(1:4)]; % The Swap
for e=1:8
op(e) = R2L2(IPI(e)); %Left 4 bit and 4bit of fk with inverse permutation.
end
disp('The 8 bit ciphertext is:')
disp(op); %this will out put the ciphertext
%%%%% Encryption Algorithm Process %%%%%%
elseif UserChoice == 2;
plaintext = input('Enter 8 bits Plaintext.use solid bracket,space between each bit and dont press enter b4 the 8th bit:');
%dec = disp(op);
In = plaintext; % The 8 bit ciphertext
%n = [1 0 1 0 0 0 1 0];
IP = [2 6 3 1 4 8 5 7]; % Initial permutation.
EP = [4 1 2 3 2 3 4 1]; % Expand permutation.
s0 = [1 0 3 2; 3 2 1 0; 0 2 1 3; 3 1 3 2]; % 4 bit input and 2 bit output
s1 = [0 1 2 3; 2 0 1 3; 3 0 1 0; 2 1 0 3];
P4 = [2 4 3 1]; % P4 permutation
IPI = [4 1 3 5 7 2 8 6]; % Inverse permutation
K = [0 1 0 1 1 1 1 1 1 1 1 1 1 1 0 0]; % K2 and K1 respectively
newK = [1 0 0 1 0 1 1 1 1 1 0 0 1 1 0 ];
%fk one of the mysterious block starts here and this is the second one
for e=1:8
LR0(e) = In(IP(e)); % Initial permutation
end
L0 = LR0(1:4); %left 4 bit
R0 = LR0(5:8); %Right 4 bit
z = 0;
for s = 1: 2
for e=1:8
ER0(e) = R0(EP(e)); %Expansion of right 4 bit
end
a = xor(ER0,K(1+z*2:8+z*2)); %
u = s0((a(2)*2 + a(3))*4 + (a(1)*2 + a(4)) + 1);
v = s1((a(6)*2 + a(7))*4 + (a(5)*2 + a(8)) + 1);
w = [floor(u/2) rem(u,2) floor(v/2) rem(v,2)]; %to binary%
for e=1:4
b(e) = w(P4(e));
end
R1(1+z:4+z) = xor(L0,b);
L0 = R0; %S-Box
R0 = R1; %S-Box
z = z+4; % for 2nd cycle%
end
R2L2 = [R1(5:8) R1(1:4)]; % The Swap
for e=1:8
op(e) = R2L2(IPI(e)); %Left 4 bit and 4bit of fk with inverse permutation.
end
disp('The 8 bit ciphertext is:')
disp(op); %Displays the ciphertext
% second encryption.
for e=1:8
LR0(e) = In(IP(e));
end
L0 = LR0(1:4);
R0 = LR0(5:8);
z = 0;
for s = 1: 2
for e=1:8
ER0(e) = R0(EP(e));
end
a = xor(ER0,K(1+z*2:8+z*2));
u = s0((a(2)*2 + a(3))*4 + (a(1)*2 + a(4)) + 1);
v = s1((a(6)*2 + a(7))*4 + (a(5)*2 + a(8)) + 1);
w = [floor(u/2) rem(u,2) floor(v/2) rem(v,2)]; %to binary%
for e=1:4
b(e) = w(P4(e));
end
R1(1+z:4+z) = xor(L0,b);
L0 = R0;
R0 = R1;
z = z+4; % for 2nd cycle%
end
R2L2 = [R1(5:8) R1(1:4)]; % swap%
for e=1:8
op(e) = R2L2(IPI(e));
end
disp(op); % final ciphertext%
else
disp ('Unable to identify please select option 1 or 2.');
end
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
