I'm implementing an AES encryption algorithm, and I need to come up with a way to increment my iv. I'm stuck on a pretty simple question:
If I have a 16-byte, randomly generated array, how can I set a specific byte to an arbitrary value?
For example, say that I just want to arbitarily set my least-significant byte to 0xff:
<?php
$bytes = openssl_random_pseudo_bytes(16);
echo bin2hex($bytes) . "<br>";
$bytes[15] = 0xff; // arbitrarily set this byte
echo bin2hex($bytes) . "<br>";
?>
This yields output like this (clearly wrong):
9299dd089611fa47f130c4e92aaa09dc
9299dd089611fa47f130c4e92aaa0932
I'm trying to get to this output:
9299dd089611fa47f130c4e92aaa09ff
I've been at this for hours, and I just can't figure it out. Can anyone help? Thank you.
EDIT: I've also tried to output the array like this:
echo var_dump(unpack('C*', $bytes));
This yields output like this (these numbers won't cross reference to what you see above, but just focus on the last byte):
array(16) { [1]=> int(242) [2]=> int(106) [3]=> int(88) [4]=> int(109) [5]=> int(145) [6]=> int(251) [7]=> int(38) [8]=> int(54) [9]=> int(39) [10]=> int(61) [11]=> int(175) [12]=> int(183) [13]=> int(27) [14]=> int(98) [15]=> int(13) [16]=> int(106) }
array(16) { [1]=> int(242) [2]=> int(106) [3]=> int(88) [4]=> int(109) [5]=> int(145) [6]=> int(251) [7]=> int(38) [8]=> int(54) [9]=> int(39) [10]=> int(61) [11]=> int(175) [12]=> int(183) [13]=> int(27) [14]=> int(98) [15]=> int(13) [16]=> int(50) }
Shouldn't the last byte show
[16]=> int(255)