create Sbox for encryption

14 views (last 30 days)
Hussain
Hussain on 7 Dec 2022
Answered: Divyam on 7 Nov 2024 at 5:26
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

Answers (1)

Divyam
Divyam on 7 Nov 2024 at 5:26
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

Categories

Find more on Encryption / Cryptography 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!