|
To cut a long story short, I have a user "mpo" who has a folder ~mpo/.gnupg (~mpo = /home/mpo) that contains her pubring.gpg and secring.gpg files. I am trying to write a Perl CGI program using the latest OpenPGP and gpg installations to encrypt a block of text using a KeyID in her keyrings and email it back to her. Is there a way to encrypt a message using a KeyID that is in these specific files i.e. ~mpo/.gnupg/secring.gpg and ~mpo/.gnupg/pubring.gpg?
I have tried gpg --homedir ~mpo/.gnupg --list-keys and the key I want to use with ID 05C1F245 is definitely there.
I tried running the following both as a CGI and from the command-line. Removing the files names i.e
+. setting $arg{SecRing} = '/home/mpo/.gnupg'; etc. makes no difference.
Also, $arg{Compat} = 'GnuPG'; is needed to pass SecRing and PubRing paramaters.
#!/usr/bin/perl
use strict;
use CGI;
use CGI::Carp 'fatalsToBrowser';
$CGI::POST_MAX= 10*1048; # max 10K byte posts
$CGI::DISABLE_UPLOADS = 1; # no upload
use Crypt::OpenPGP;
my $mailprog = "/usr/sbin/sendmail";
my $recipient = "test\@mydomain.com";
my $key_id = "05C1F245";
my $arg;
$arg{Compat} = 'GnuPG';
$arg{SecRing} = '/home/mpo/.gnupg/secring.gpg';
$arg{Pubring} = '/home/mpo/.gnupg/pubring.gpg';
$arg{ConfigFile} = '/home/mpo/.gnupg/gpg.conf';
my $pgp = Crypt::OpenPGP->new(%arg);
my $ciphertext = $pgp->encrypt(
Data => "test 1234",
Recipients => $key_id,
Armour => 1,
);
print $pgp->errstr;
open (MAIL,"|$mailprog $recipient") || die "Unable to send email to $recipient.";
print MAIL "$ciphertext\n";
close (MAIL);
exit;
The above returns the error "No known recipients for encryption".
I have also tried this (via the command-line and as a CGI) and it gives the same error:
#!/usr/bin/perl
use strict;
use CGI;
use CGI::Carp 'fatalsToBrowser';
$CGI::POST_MAX= 10*1048; # max 10K byte posts
$CGI::DISABLE_UPLOADS = 1; # no upload
use Crypt::OpenPGP;
use Crypt::OpenPGP::KeyRing;
my $mailprog = "/usr/sbin/sendmail";
my $recipient = "test\@mydomain.com";
my $key_id = "05C1F245";
my $arg;
$arg{Compat} = 'GnuPG';
my $secring = Crypt::OpenPGP::KeyRing->new(Filename => '/home/mpo/.gnupg/secring.gpg');
my $pubring = Crypt::OpenPGP::KeyRing->new(Filename => '/home/mpo/.gnupg/pubring.gpg');
$arg{SecRing} = $secring;
$arg{Pubring} = $pubring;
my $pgp = Crypt::OpenPGP->new(%arg);
my $ciphertext = $pgp->encrypt(
Data => "test 1234",
Recipients => $key_id,
Armour => 1,
);
print $pgp->errstr;
open (MAIL,"|$mailprog $recipient") || die "Unable to send email to $recipient.";
print MAIL "$ciphertext\n";
close (MAIL);
exit;
Please can someone let me know what I am doing wrong or if indeed what I am trying to do is possible. Or of there is a work-around or something. Thanks in advance.
Kind regards,
Ata
|