Thread

Posted on Mon Feb 21 21:14:30 2005 by ata
Help with Crypt::OpenPGP
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
Direct Responses: 140 | Write a response
Posted on Mon Feb 21 22:03:39 2005 by ata in response to 139
Re: Help with Crypt::OpenPGP
OK, as usual when all else fails ignore the documentation and look at an example. This works. BTW, the process must of course have read access to the keyrings.
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 $ring_file = '/home/mpo/.gnupg/pubring.gpg'; my $ring = Crypt::OpenPGP::KeyRing->new( Filename => $ring_file ) or die "Error: Crypt::OpenPGP::KeyRing->errstr"; my $pgp = Crypt::OpenPGP->new(PubRing => $ring); 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;
Direct Responses: 2614 | Write a response
Posted on Wed Jul 12 19:13:31 2006 by builder in response to 140
Re: Help with Crypt::OpenPGP
Would you know a good source of information to help me implement OpenPGP encryption in my perl application? The documentation available online seems a little thin. A very simple example does not seem to be available anywhere.

For instance, I have a public key and some data (a report drawn from a database in csv format a.k.a. text). I want to use the public key to encrypt the data then stuff it in a file. Simple. Nothing fancy or "trick", but ...

So, I start reading the OpenPGP module docs. Keyring? What the hell do I need another file dependency for? Alright. Create a subroutine to take the public key and create a keyring file. How? There's documentation all over the web and none of it is to the point.

data + encryption key + encryption algorithm = encrypted data

Would seem rather straight forward.

Thank you for any enlightenment you wish to share with this feeble mind.
Direct Responses: 2635 | Write a response
Posted on Mon Jul 17 18:18:45 2006 by builder in response to 2614
Re: Help with Crypt::OpenPGP
I'm trying to write a subroutine to GnuPG encrypt a report (generated from a database query) given only the report contents and the encryption key as arguments. No external dependencies (other than Perl modules and specifically excluding GPG binaries and its keyring files).

This looks like it should work (I had help from Benjamin Trott, but I guess, I don't fully understand his comments).

Does anyone have any other bright ideas on how to eliminate these external dependencies?
#!/usr/bin/perl #================================================================================ # test script to encrypt data without external dependencies #================================================================================ use strict; use warnings; use Crypt::OpenPGP; use Crypt::OpenPGP::KeyBlock; use Crypt::OpenPGP::KeyRing; my $plaintext = qq^ Dummy report data ^; my $ring = Crypt::OpenPGP::KeyRing->new( Data => "-----BEGIN PGP PUBLIC KEY BLOCK----- Version: PGP 6.5.1 mQGiBES0BisRBAD11RjxN2lk+2eKsVjfJSrjkrbhoIRS1eduMb1Q1dzbqNAG1L/j Srd+T/dfWEqWu0LqBwgSK/P6a+cdnENSFj8LQOWD8zbrXbok2gFoIkF/L1O/7znC 5IwFPP+2FZe1kFyY36PKooot+okYG/G16HNAuU3CzxBYCuqkxqEDeU1kdwCg/1jI FrbO0cUrJrit/6NIyzDBCOMEANgBXtHtdrdtmwE35HcFO2sdywvvIOE+L1Wouwku ewK6/bpLGa1LnYu7T7xO+CziYqjp9SIGiSPmZjQQPXfBrEtCvLMCTC/mdylOP6Sh hrnLWgPgAV5d7wpR1VUQCugDDZ9s/ExQuQEeD3dHt2dBBco5/+lvdr/cXE06J3x2 fETgA/9dhSfdp4h4PIKYkgDvTGIpFp6JeKT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YhEBPFCgsjEqKu0TXWDVkkzzxLO11Uq6VTYt/ve1cVz mq2vLN3km1kZoumXHYROV9mg2Zr7Y6604r+OAVHxNgJz5X6OB36bjQgpWDPuNWmk uh1PrZAT6WB2LJDaSZk6y3EQfGWvnZ5OqgqhIVl2HlyMGU4E8C8iTkuCStpndMFw ZchMKR7UbTswzI7m4t4RPLvpaF8YxmAJy9UzZEK3Ycnq5oNsgicKqgM2GVpky1GO oAtMaIkARgQYEQIABgUCRLQGLwAKCRDs+BYScUQ0FoObAKD1vGbfNCeu/CVvwsTr 9mz6nUmeTgCeNO9P7OkUuySN6D2YTYbKvgIAEes= =+ksY -----END PGP PUBLIC KEY BLOCK-----" ); my $kb = Crypt::OpenPGP::KeyBlock->new; $kb->add( ($ring->blocks)[0] ); my $cert = $kb->encrypting_key; my $pgp = Crypt::OpenPGP->new( Compat => 'GnuPG' ); my $ct = $pgp->encrypt( Key => $cert, Data => $plaintext ) or die "ERROR: " . $pgp->errstr; print $ct;
Direct Responses: 2650 | Write a response
Posted on Wed Jul 19 18:07:46 2006 by builder in response to 2635
Re: Help with Crypt::OpenPGP
The error message is not very enlightening (at least to me)
[builder@engproto crypt]$ perl crypt ERROR: Need something to encrypt with [builder@engproto crypt]$
Direct Responses: 2659 | Write a response
Posted on Fri Jul 21 17:37:37 2006 by builder in response to 2650
Re: Help with Crypt::OpenPGP
This script works thanks to a lot of help from Benjamin Trott.
#!/usr/bin/perl use strict; use warnings; use Crypt::OpenPGP; my $ring = Crypt::OpenPGP::KeyRing->new( Data => qq^-----BEGIN PGP PUBLIC KEY BLOCK----- Version: PGP 6.5.1 mQGiBES0BisRBAD11RjxN2lk+2eKsVjfJSrjkrbhoIRS1eduMb1Q1dzbqNAG1L/j Srd+T/dfWEqWu0LqBwgSK/P6a+cdnENSFj8LQOWD8zbrXbok2gFoIkF/L1O/7znC 5IwFPP+2FZe1kFyY36PKooot+okYG/G16HNAuU3CzxBYCuqkxqEDeU1kdwCg/1jI FrbO0cUrJ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aL2RDK4xq/E oIERKZftsI2TvglcgNrw8NXnps1hT5lP1oEjmRF8B57bm4p4fScfaPEodzCTsbX7 KFI+ie/XbApHrcFnQzfQFA0hqDFidATSxf6mWPPMUVSvDTaNbq1Ps88OLeSoF3kE 58hMm9x8xZJOnQeTe4gvX2uDz+F6daxC7xBd73axc2KS9mnBqj9Xw2qLm438TuKC V7ioOXxzeSVioDNsnh+PLYhEBPFCgsjEqKu0TXWDVkkzzxLO11Uq6VTYt/ve1cVz mq2vLN3km1kZoumXHYROV9mg2Zr7Y6604r+OAVHxNgJz5X6OB36bjQgpWDPuNWmk uh1PrZAT6WB2LJDaSZk6y3EQfGWvnZ5OqgqhIVl2HlyMGU4E8C8iTkuCStpndMFw ZchMKR7UbTswzI7m4t4RPLvpaF8YxmAJy9UzZEK3Ycnq5oNsgicKqgM2GVpky1GO oAtMaIkARgQYEQIABgUCRLQGLwAKCRDs+BYScUQ0FoObAKD1vGbfNCeu/CVvwsTr 9mz6nUmeTgCeNO9P7OkUuySN6D2YTYbKvgIAEes= =+ksY -----END PGP PUBLIC KEY BLOCK-----^ ); my $datafile = "test_data.csv"; open( INFILE, "< $datafile" ) or die "Could not open csv file - $!"; my $plaintext = <INFILE>; close INFILE; $ring->read; my $kb = $ring->find_keyblock_by_index(0); my $cert = $kb->encrypting_key; my $pgp = Crypt::OpenPGP->new( Compat => 'GnuPG' ); my $ct = $pgp->encrypt( Key => $cert, Data => $plaintext ) or die "ERROR: " . $pgp->errstr; open( OUTFILE, "> $data_file.pgp" ) or die "Could not open file for encrypted data - $!"; print OUTFILE $ct; close OUTFILE;
Write a response