How to apply S-box on input data?

9 views (last 30 days)
lilly lord
lilly lord on 25 Jun 2022
Commented: Voss on 27 Jun 2022
Hello, I have an S-box and I want to pass few numbers through S-box
if x=0,1,2,3,4,5,6,7 then S-box(x)=0,1,3,6,7,4,5,2
A=0:7;
Sbox=[0,1,3,6,7,4,5,2];
X=de2bi(A,3,'left-msb');
U=[];
res=[];
w1=[];
for i=1:length(A)
U(i,:)=xor(X(i,:),X(2,:));
res=bi2de(U,'left-msb')'
end
s_out=[];
for i=1:8
s_out = sbox(res(i));% error in this line
end
res=[1,0,3,2,5,4,7,6];
Apply S-box on res. Desired outcome for S-box(1)=1, S-box(0)=0, S-box(3)=6, S-box(2)=3, S-box(5)=4, S-box(4)=7, S-box(7)=2 and S-box(6)=5

Accepted Answer

Voss
Voss on 25 Jun 2022
Edited: Voss on 25 Jun 2022
A=0:7;
Sbox=[0,1,3,6,7,4,5,2];
N = length(A);
X=de2bi(A,3,'left-msb');
U=[];
% res=[];
% w1=[];
for i=1:N
U(i,:)=xor(X(i,:),X(2,:));
% res=bi2de(U,'left-msb')'
end
res=bi2de(U,'left-msb')'; % calculate res once, for all U, after the loop
s_out=[];
for i=1:N
% s_out = sbox(res(i));% error in this line
s_out(i) = Sbox(res(i)+1); % - the variable is Sbox, not sbox (MATLAB is case-sensitive)
% - add 1 to res(i) to use as index into Sbox (res starts at 0, index start at 1)
% - assign the result to s_out(i), i.e., a single element of s_out
end
s_out
s_out = 1×8
1 0 6 3 4 7 2 5

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!