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:');
disp(sbox_output);