At 9/17/01 9:40 PM, Vladislav Vik wrote:

>I would like to add subroutine for PGP encryption of the orders e-mail.
>
>However I do not have a clue how to do it. If anybody has an idea on how to
>do it or even better is willing to share the code that will do that. Please
>let me know.

[Changed list to [EMAIL PROTECTED], which is for discussing code.]

I use GnuPG instead of PGP, and at least for GnuPG, this is harder than 
it looks. There are three perl modules that claim to work; in my testing, 
all three of them had some kind of problem.

I eventually had to resort to opening a pipe to GPG and having it write 
an encrypted file. You could do something more complicated with expect or 
open2, but I've found the following to be reliable for GnuPG 1.0.6 and 
Red Hat Linux 6.2 and 7.1:

sub GPGEncode
{
        my $plaintext = shift;
        
        # directory with GPG keys
        my $homedir = '/usr/local/etc/gnupg';

        my $key = '[EMAIL PROTECTED]';
        
        my $file = `mktemp -q /tmp/GPG.XXXXXX`
                or die "Can't create temporary file: $!\n";
        $file =~ /^(\/tmp\/GPG\.\w\w\w\w\w\w)$/
                or die "Can't untaint temporary file\n";
        $file = $1;
        
        open (OUTFILE, "| /usr/bin/gpg --no-secmem-warning --batch 
--no-tty --textmode --encrypt --homedir '$homedir' --recipient '$key' 
--armor >> $file")
                or die "Can't open GPG: $!\n";
        
        print OUTFILE $plaintext or die "Can't print: $!\n";
        close OUTFILE or die "Can't close: $!\n";
        
        open INFILE, $file or die "Can't open $file: $!\n";
        my $result = join "", <INFILE>;
        close INFILE;
        
        unlink ($file) or warn "Can't unlink temp file: $!\n";
        
        return $result;
}

Just use it like:

  my $encryptedText = GPGEncode 'Testing';

If anyone has anything better (or sees anything wrong with this), please 
do share.

--
Robert L Mathews, Tiger Technologies

Reply via email to