Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Crypt::OpenPGP, encrypting messages using a public key file

by BUU (Prior)
on Jan 12, 2006 at 02:14 UTC ( [id://522597]=perlquestion: print w/replies, xml ) Need Help??

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

My situation is fairly simple. I want to encrypt a file using PGP and a public key contained in a specific file. Following the Crypt::OpenPGP synopsis leads me to this code:
use Crypt::OpenPGP; my $keyring = Crypt::OpenPGP::KeyRing->new( Filename => "foo.asc" ); my $pgp = Crypt::OpenPGP->new( PubRing => $keyring ); my $data = $pgp->encrypt( Filename => $filename );
Unfortunately this results in no data being placed in $data, I assume because I didn't specify a 'Recipients' field to the encrypt method. Reading the documentation for encrypt tells me that Recipients should take one of an 8-digit hex key id, a 16 digit hex key id, or a user id. My problem is, I have no idea what to pass here. Is this a value I should be retrieving from the file some how? Is there a way to get it from the file? If not, where should I get it?

Replies are listed 'Best First'.
Re: Crypt::OpenPGP, encrypting messages using a public key file
by edoc (Chaplain) on Jan 12, 2006 at 02:44 UTC
    When your public key was generated it would have had an 'Identity'. You want to use this (or part of it) as the 'Recipients'.
    #!/usr/bin/perl use strict; use warnings; # generate public/private Crypt::OpenPGP keys. # encrypt some data # decrypt some data use Crypt::OpenPGP; my $size = 1024; my $ident = 'Me <me@example.com>'; my $pass = 'my passphrase'; my $public_file = 'public.pgp'; my $private_file = 'private.pgp'; my $keychain = Crypt::OpenPGP->new; my ($public, $private) = $keychain->keygen ( Type => 'RSA', Size => $size, Identity => $ident, Passphrase => $pass, Verbosity => 1, ) or die $keychain->errstr( +); my $public_str = $public->save; my $private_str = $private->save; print "Public encrypting_key: ".$public->encrypting_key ."\n"; print "Private encrypting_key: ".$private->encrypting_key ."\n"; open( PUB, '>', $public_file ) or die $!; print PUB $public_str; close(PUB); open( PRIV, '>', $private_file ) or die $!; print PRIV $private_str; close(PRIV); my $pgp = Crypt::OpenPGP->new( PubRing => $public_file ); my $cyphertext = $pgp->encrypt ( Data => 'Encrypt This', Recipients => $ident, Armour => 1, ) || die $pgp->errstr(); print $cyphertext; $pgp = new Crypt::OpenPGP( SecRing => $private_file ); my $plaintext = $pgp->decrypt ( Data => $cyphertext, Passphrase => $pass, ) || die $pgp->errstr(); print "plaintext: $plaintext\n";

    cheers,

    J

Re: Crypt::OpenPGP, encrypting messages using a public key file
by hv (Prior) on Jan 12, 2006 at 09:58 UTC

    The user id is typically the email address for which the PGP key was generated. Here's the code I use, which targets someone for whom I have a PGP public key block and an email address:

    sub encrypt { my($proto, $target, $data) = @_; my $id = $target->email; my $key = $target->pgp_key; my $ring = Crypt::OpenPGP::KeyRing->new(Data => $key) or $proto->error("new user KeyRing failed"); my $pgp = Crypt::OpenPGP->new(PubRing => $ring) or $proto->error("new Context failed"); my $kb = $ring->find_keyblock_by_uid($id) or $proto->error("find user keyblock failed", $ring); my $alg = $kb->preferred_sk_alg; $pgp->encrypt( Data => $data, Recipients => [ $id ], Armour => 1, ($alg ? (Cipher => $alg) : ()), # use default if no preference + located ); }

    Note that this is also using Crypt::OpenPGP - finding and using preferred SK algorithm to get an appropriate encryption mechanism.

    Hugo

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://522597]
Approved by McDarren
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-23 22:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found