create Sbox for encryption

14 次查看(过去 30 天)
Hussain
Hussain 2022-12-7
回答: Divyam 2024-11-7,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

回答(1 个)

Divyam
Divyam 2024-11-7,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

类别

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