#!/usr/bin/perl

sub file_crypt {
    my ( $alg, $key, $iv, $mode, $filein, $fileout ) = @_;

    my $enc = SSLeay::Cipher::new( $alg );
    $enc->init( $key, $iv, $mode );

    my $buf;

    while ( sysread( $filein, $buf, 1024 ) ) {
        print $fileout $enc->update($buf);
    }
    print $fileout $enc->final;
}

# Encrypt
open IN, "<file.plain";
open OUT, ">file.enc";
file_crypt( 'idea-ecb', (pack "H16", "0123456789abcdef"), undef,
    1, *IN, *OUT );
close IN;
close OUT;

# Decrypt
open IN, "<file.enc";
open OUT, ">file.dec";
file_crypt( 'idea-ecb', (pack "H16", "0123456789abcdef"), undef,
    0, *IN, *OUT );
close IN;
close OUT;

# "file.plain" should now be the same as "file.dec"
