create Sbox for encryption

조회 수: 13 (최근 30일)
Hussain
Hussain 2022년 12월 7일
답변: Divyam 2024년 11월 7일
Good day,
How can I convert a series of 64-bit to (16 decimal) vector to create sbox,
for example, I need the output like this ... sbox=[12 5 6 11 9 0 10 13 3 14 15 8 4 7 1 2];
key input as (64-bit binary)
I'm write the below code
function s_out=updatesbox(key)
sbox=[];
for i=1:4:64
sbox[] = bin2dec(key(i:i+3)+1);
end
thanks

답변 (1개)

Divyam
Divyam 2024년 11월 7일
You should add input validation to your code to ensure that the key is a string of 64 characters. The offset used in the "bin2dec" function is unnecessary considering that it does not add any value to the crytogrpahic strength.
To solve the indexing problem, you can use direct indexing with to correctly place each decimal value in the "sbox" array.
function s_out = updatesbox(key)
% Ensure the key is a string of 64 characters
if length(key) ~= 64
error('Key must be a 64-bit binary string.');
end
sbox = zeros(1, 16); % Preallocate space for the S-box
for i = 1:4:64
% Convert 4-bit binary substring to decimal
decimal_value = bin2dec(key(i:i+3));
% Add 1 to the decimal value to match your desired output
sbox((i+3)/4) = decimal_value;
end
s_out = sbox;
end
% Sample 64-bit binary input
key = '1100101011110000101010111100110010101011110000101010111100110010';
% Call the updatesbox function
sbox_output = updatesbox(key);
% Display the resulting S-box
disp('Generated S-box:');
Generated S-box:
disp(sbox_output);
12 10 15 0 10 11 12 12 10 11 12 2 10 15 3 2

카테고리

Help CenterFile Exchange에서 Encryption / Cryptography에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by