Increment Nonce by any fixed value in loop

I have the nonce value in hex. Let say:
nonce = {'00' '11' '22' '33' '44' '55'};
I need to add any fixed value 'x'(hex) to this value in loop and need to save every new value.
I was doing this like:
nonce = {'00' '11' '22' '33' '44' '55'};
for i=1:5
nonce(i) = nonce + 01;
i = i+1;
end
But I am not acheiving what I need.

1 Kommentar

Please mention, what you expect as output. Having only not running code does not clarify, what you want to achieve.
Your code contains some bugs:
for i=1:5
nonce(i) = nonce + 01;
% Here on the left side there is a scalar cell element, and on
% the right you try to add 01 (which is the same as 1) to a
% cell vector.
i = i+1; % i is iterated by the FOR loop already
% so do not increase it manually
end

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Alex Mcaulley
Alex Mcaulley am 24 Jun. 2019

0 Stimmen

Using a loop:
nonce = {'00' '11' '22' '33' '44' '55'};
x = '01';
for i = 1:numel(nonce)
nonce{i} = dec2hex(hex2dec(nonce{i}) + hex2dec(x),2);
end
Or using cellfun:
nonce = {'00' '11' '22' '33' '44' '55'};
x = '01';
nonce = cellfun(@(y) dec2hex(hex2dec(y) + hex2dec(x),2),nonce,'uni',0);

3 Kommentare

Zeeshan Abbas
Zeeshan Abbas am 25 Jun. 2019
Thank you but it's giving me: nonce = {'01' '12' '23' '34' '45' '56'}
I need it to be: nonce = {'00' '11' '22' '33' '44' '56'}; just need to increament the whole value by 1, not every element. We can increament it from deecimal value as well and then at last convert it into hex. But I need to increment the value 5-times and need to save every value.
The ultimate usage is to use this as a nonce/counter value in CTR mode block cipher to create bit stream.
Well, then an option is:
nonce = {'00' '11' '22' '33' '44' '55'};
x = '01';
a = strcat(nonce{:});
for i = 2:6
a(i,:) = dec2hex(hex2dec(a(i-1,:)) + hex2dec(x),12);
end
nonce = mat2cell(a,ones(6,1),2*ones(6,1))
nonce =
6×6 cell array
'00' '11' '22' '33' '44' '55'
'00' '11' '22' '33' '44' '56'
'00' '11' '22' '33' '44' '57'
'00' '11' '22' '33' '44' '58'
'00' '11' '22' '33' '44' '59'
'00' '11' '22' '33' '44' '5A'
At the end of the code, each row of nonce corresponds with the value in each iteration.
Zeeshan Abbas
Zeeshan Abbas am 26 Jun. 2019
Thank you very much. It's giving what I asked, so I am accepting. Although it don't worked for me in that scenario in which I was seeking this.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Jan
Jan am 25 Jun. 2019
Bearbeitet: Jan am 25 Jun. 2019

0 Stimmen

nonce = {'00' '11' '22' '33' '44' '55'};
toAdd = hex2dec('01');
for k = 1:5
nonce{k} = dec2hex(hex2dec(nonce{k}) + toAdd);
end
It might be easier to work with a double vector, if you want to perform calculations. Then hex strings are rather inefficient.

Kategorien

Mehr zu Encryption / Cryptography finden Sie in Hilfe-Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by