sub read_bytes { my ($fh, $to_read) = @_; $buf = ''; while ($to_read) { my $bytes_read = read($FH_decrypted, $buf, length($buf)); die("$!\n") if !defined($bytes_read); die("Unexpected end of file\n") if !$bytes_read; $to_read -= $bytes_read; } return $buf; }