Jason Balicki wrote:
> 
> This is a piece of code taken from a larger program.
> 
> I'm trying to determine that the encryption succeded, but when
> I try to evaluate the code, it always evaluates false even though
> the encryption works and produces an encrypted file.
> 
> Here's the specific bit I'm concerned with:
> 
> if ($gpg->encrypt ( plaintext => $infile, output => $outfile, recipient => 
> $recipient )){
>         $encrypt_status = "success";
> }
> 
> This always evaluates as false and never sets $encrypt_status to "success" 
> even
> though the encryption does, in fact, succeed.

The documentation for GnuPG says:

<quote>
encrypt( [params] )

  This method is used to encrypt a message, either using assymetric or
  symmetric cryptography. The methods croaks if an error is encountered.
  Parameters:

  plaintext

    This argument specifies what to encrypt. It can be either a filename or a
    reference to a file handle. If left unspecified, STDIN will be encrypted.

  output

    This optional argument specifies where the ciphertext will be output. It
    can be either a file name or a reference to a file handle. If left
    unspecified, the ciphertext will be sent to STDOUT.
</quote>


> And here it is in context:
> 
> #!/usr/bin/perl
> use GnuPG;
> use strict;
> use warnings;
> 
> my $recipient = "[EMAIL PROTECTED]";
> 
> my $infile = $ARGV[0];
> my $outfile = $ARGV[1];
> my $encrypt_status = "failed";
> 
> if ( -z $infile) {

-z returns TRUE if the file contains ZERO bytes, and then you try to open a
file that exists but is empty?

>         open (PLAIN, "<<$infile") or die "Can't open $infile for reading!";

There is no '<<' mode so if for example $infile contains 'file' you are trying
to open the file named '<file'.  You should use the three argument form of
open and include the $! variable in the error message.

open PLAIN, '<', $infile or die "Can't open $infile: $!";


> }
> open (ENCRYPT, ">>$outfile") or die "Can't open $outfile for writing!";

Do you really want to open the file for appending?

open ENCRYPT, '>', $outfile or die "Can't open $outfile: $!";


> my $gpg = new GnuPG( );
> 
> if ($gpg->encrypt ( plaintext => $infile, output => $outfile, recipient => 
> $recipient )){

GnuPG's encrypt() allows you to use file names, in which case you don't have
to open the files, or to open the files first and use the filehandles.  Since
you already have the files open you can use the filehandles:

$gpg->encrypt(
    plaintext => \*PLAIN,
    output    => \*ENCRYPT,
    recipient => $recipient,
    );

The documentations says "The methods croaks if an error is encountered." so in
order to determine success or failure you have to wrap the code in an eval 
block:

eval {
    $gpg->encrypt(
        plaintext => $infile,
        output    => $outfile,
        recipient => $recipient,
        );
    };

print "encrypt status: [EMAIL PROTECTED]";




John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to