How to apply S-box on input data?

조회 수: 8 (최근 30일)
lilly lord
lilly lord 2022년 6월 25일
댓글: Voss 2022년 6월 27일
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
  댓글 수: 2
lilly lord
lilly lord 2022년 6월 27일
Thank you very much sir
Voss
Voss 2022년 6월 27일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (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