How to apply S-box on input data?

8 次查看(过去 30 天)
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

采纳的回答

Voss
Voss 2022-6-25
编辑:Voss 2022-6-25
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by