Using OpenPGP Encryption Within Perl

   Print.Print
Email.Email weblog link
Blog this.Blog this
Chris Josephes

Chris Josephes
Aug. 22, 2005 04:54 AM
Permalink

Atom feed for this author. RSS 1.0 feed for this author. RSS 2.0 feed for this author.

URL: http://search.cpan.org/~btrott/Crypt-OpenPGP-1.03/...

There are a few perl modules and tools out there that can be used to encrypt data using PGP or GPG. Most of them require that the end user have an actual pgp or gpg executable program that can be accessed by a system call or an IPC pipeline. But there is one module that stands out from the rest.

Crypt::OpenPGP is a pure perl implementation of the OpenPGP standard. It can support different versions of PGP and GPG encryption. Using this distribution will not require a command line program for a back-end.

Here's a short example of sending an outgoing message that will be signed using an existing GPG key.

#!/usr/local/bin/perl

#
# Includes
use Mail::Internet;
use Crypt::OpenPGP;

our $headers=[
     "From: admin@example.com",
     "To: enduser@example.com",
];

our $msg=<<EOM
Thank you for filling out our survey.
We will be emailing you shortly.

EOM
;

our $openpgp=Crypt::OpenPGP->new(Compat => "GnuPG");

our $sig=$openpgp->sign( Data => $msg,
        KeyID => "4FCA4C4D",
        Passphrase => "testphrase1",
        Armour => 1);

$msg.=$sig;

our $mailer=Mail::Internet->new($headers);
$mailer->body($msg);

$mailer->smtpsend();

exit 0;

One disadvantage of this method is that it requires about 15 dependency modules to be installed. There isn't a Bundle class to make the installation easy, but you can still get Crypt::OpenPGP up and running within an hour.

Chris Josephes works as a system administrator for Internet Broadcasting.