Decryption using GnuPG:Interface and Perl

christopher.a.feathers@accenture.com christopher.a.feathers@accenture.com
Mon Jun 18 17:00:02 2001


I am trying to wrap some of the GnuPG functions in Perl so I can enforce
some logging (an a few other reasons).  l am running into a problem when I
attempt decrypt an pass in a bad passphrase - a scenario that is quite
possible.  I get a "Broken pipe" printed to STDOUT and my script cannot
continue.  Is there a way to catch the fact that the passphrase was bad
before attempting to print the encrypted file's contents to stdin as given
in the GnuPG::Interface examples?

Here is the code with a comment where I am getting the broken pipe

<BEGIN CODE>
sub decrypt_input_file {

     $input = IO::Handle->new();
     $output = IO::File->new( ">$output_file");
     $errfile = IO::File->new( ">>$TEMP_STDERR");
     $passphrase_fh = IO::Handle->new();

     $handles = GnuPG::Handles->new(stdin=>$input, stdout=>$output, stderr
=>$errfile, passphrase=>$passphrase_fh);
     $handles->options( 'stdout' )->{direct} = 1;
     $handles->options( 'stderr' )->{direct} = 1;

     $gnupg = GnuPG::Interface->new();

     $cipher_file = IO::File->new("<$input_file");

     $pid = $gnupg->decrypt(handles=>$handles);
     print $passphrase_fh $passphrase;
     close $passphrase_fh;

     # The next line is the one that gives me the problem if the passphrase
is bad
     print $input $_ while <$cipher_file>;

     close $input;
     close $cipher_file;
     close $errfile;

     waitpid $pid, 0;

     close $output;
     return (1);
}
<END CODE>

 -Chris