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


In reply to Re: crypt::openpgp encrypt error by hv
in thread crypt::openpgp encrypt error by boat73

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.