boat73 has asked for the wisdom of the Perl Monks concerning the following question:

Hello all. I have been working on this much longer than I care to admit so I am sure I am missing something simple. I have created a public and private key file with Crypt::OpenPGP using the following code.
use Crypt::OpenPGP; $pgp = Crypt::OpenPGP->new (); %attrib = ( 'Type' => 'DSA', 'Size' => '1024', 'Identity' => 'Garett L Holmes <email address here>', 'Passphrase' => 'password', 'Cipher' => 'DES3', 'Verbosity' => '1' ); ($id, $email) = split("<", $attrib{Identity}); $id =~ s/\s+/_/g; chop $id; ($pubkey, $privkey) = $pgp->keygen( %attrib ) or die $pgp->errstr; $public = $pubkey->save; open(PUBLIC,">${id}.public") or die "COULD NOT OPEN ${id}_pub.pgp\n"; print PUBLIC $public; close(PUBLIC); $private = $privkey->save; open(PRIVATE,">${id}.private") or die "COULD NOT OPEN ${id}_priv.pgp\n +"; print PRIVATE $private; close(PRIVATE);
That seems to create the files no problem but when I try to encrypt using the following code...
use Crypt::OpenPGP; my $pgp = Crypt::OpenPGP->new ( "PubRing" => 'pubring.pkr' )or die Crypt::OpenPGP->errstr; my $ciphertext = $pgp->encrypt ( "Compat" => 'PGP5', "Data" => 'this is a test', "Recipients" => 'Garett L Holmes', "Armour" => 1, );<br> die "Encryption failed: ", $pgp->errstr unless $ciphertext; print $ciphertext;
I get this error
<code>"Encryption failed: No known recipients for encryption"<code>
I know the encrypt code works as I used it with another valid key file. Any help with this is appreciated.

Edited by BazB: fixed/added code tags. Removed email address.

Replies are listed 'Best First'.
Re: crypt::openpgp encrypt error
by hv (Prior) on Mar 12, 2004 at 00:44 UTC

    As far as I understand it (which isn't very far), the recipients are expected to be specified by "id", and I believe that by default when you generate a key it picks the email address you specify to be the id.

    Note also that recipients is plural, and normally expects an arrayref of recipients; I'm pretty sure though that this isn't your problem, that the code copes with a single recipient supplied in non-arrayref form.

    Here is (roughly) the code I use for encryption, which fetches the key from elsewhere:

    sub encrypt { my($proto, $target, $data) = @_; my $id = $target->email_address; my $key = $target->pgp_key; my $ring = Crypt::OpenPGP::KeyRing->new(Data => $key) or die "new KeyRing failed: $Crypt::OpenPGP::ErrorHandler: +:ERROR"; my $pgp = Crypt::OpenPGP->new(PubRing => $ring) or die "new Context failed: $Crypt::OpenPGP::ErrorHandler: +:ERROR"; $pgp->encrypt( Data => $data, Recipients => [ $id ], Armour => 1, ); }

    However it took me a while to get there, and the maze of interconnected modules can be daunting to read through. Certainly I found it very helpful to install the modules to a personal installation of perl so I could safely stuff extra diagnostics directly into the modules to help explain what was going on.

    One thing in particular you can do to check whether the recipient id can be found in the keyring looks something like this:

    $ring->find_keyblock_by_uid($id) or die "Find keyblock failed: @{[ $ring->errstr ]}";

    Hope this helps,

    Hugo

      Thanks for the reply. Gets me that much closer
Re: crypt::openpgp encrypt error
by Thelonius (Priest) on Mar 11, 2004 at 20:14 UTC
    In the first step, you create a file called "Garett_L_Holmes_.public". In the second, you use a keyring file called "pubring.pkr". Did you rename the file or concatenate the first into pubring.pkr? I don't see how Crypt::OpenPGP is supposed to find the key.
      I renamed the file in an attempt to get it to work. The actual code hase
      "PubRing" => 'Garett_L_Holmes.public'
      Sorry for the error.
Re: crypt::openpgp encrypt error
by waswas-fng (Curate) on Mar 11, 2004 at 20:37 UTC
    Also you created the Id above as "Garett L Holmes <gaholmes@hotmail.com>" and used the recipiant below as "Garett L Holmes", I have not used that module before, is it smart (dumb???) enough to know what you mean?


    -Waswas
      I have tried it both ways without success.
        Srry forgot to log in. The last two Anonymous Monk entries are mine.