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

    Yes. That was the issue. Thank you kindly. An encryption program, naturally, must expect to read in files of whatever type.

    What I'm hoping to do is recreate in Perl an encryption program which I had written long decades past and subsequently lost. The original was among many which I had authored in JForth on an Amiga 2000. Those I had preserved for ten years or more on a CDROM but now cannot find that disk anywhere.

    I have all but entirely forgotten Forth, and my Perl has also grown rusty. Being now retired, I have elected to refresh myself in both as hobby projects. Again, I thank you for your indulgence.

      Note that my padding algorithm may not be optimal from a cryptographic standpoint. I have not researched the padding algorithms used in crypto. But I doubt you care :)

      Also, you've confirmed that I doesn't make sense. The encryption should work the same on every machine. You want L< (or L>) or Q< (or Q>). See Mini-Tutorial: Formats for Packing and Unpacking Numbers.