You can use the "read" function to pull in any reasonable number of bytes you like into a scalar, and loop over substrings of the scalar to build up the corresponding number of output cipher bytes and print that to output.
I don't know enough about Twofish_PP (or other modules or conventions associated with this algorithm) to say what sort of formatting issues are assumed. Does the fact that it only deals with 16 bytes at a time mean that the output should be structured as lines of 16 characters terminated by newlines? Does this matter?
In any case, you also need to be careful when you reach the end-of-file, because chances are good the file size won't be a multiple of 16, and you'll need to pad the string. For example (not tested):
open( IN, "big.file" ) or die $!;
while ( read( IN, my $cleartxt, 8192 ) > 0 ) {
my $cryptxt = "";
while ( $cleartxt ) {
$tocrypt = substr( $cleartxt, 0, 16 );
$cleartxt = substr( $cleartxt, 16 );
if ( length( $tocrypt ) != 16 ) {
$tocrypt .= " " x ( 16 - length( $tocrypt ));
}
# do what you need with Twofish to encrypt, and
# append result to $cryptxt
}
print $cryptxt;
}
|