in reply to Re^3: binmode copy loses final byte
in thread binmode copy loses final byte
Then give it a valid input file! Your program needs a file that consists of a sequence of "packed" int objects, but you didn't provide such a file. Put differently, you need a file whose size is a multiple of sizeof(int).
Or, from the other perspective, you wrote a program that needs a file whose size is a multiple of sizeof(int), but that's not what the program should expect.
When using a block ciphers on a stream of arbitrary length, a padding algorithm is used to make the input size a multiple of the block size before encryption.
In your case, you could use the following:
my $padding_len = $int_size - ( length( $buf ) % $int_size ); my $padding = ( "\x00" x ( $padding_len - 1 ) ) . chr( $padding_len ); $buf .= $padding;
After decrypting, you remove the padding by removing an amount of bytes equal to the value of the last bytes.
$buf = substr( $buf, 0, -ord( substr( $buf, -1 ) ) );
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: binmode copy loses final byte
by aplonis (Pilgrim) on Jul 01, 2025 at 13:33 UTC | |
by ikegami (Patriarch) on Jul 01, 2025 at 15:07 UTC |