Solved

Crypt::OpenPGP without external dependencies

Posted on 2006-07-18
6
252 Views
Last Modified: 2008-03-06
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 concept test script looks like it should work (I had help from the module author, but I guess, I don't fully understand his comments).  Does anyone have any other bright ideas?


#!/usr/bin/perl

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/9dhSfdp4h4PIKYkgDvTGIpFp6JeKTVd5PnTu80JO9JkkSWtPryukDT/smq
ACzyrCkexY0zdGgFJ5mmrK/S5KNyrUOw46noCj07k6/l4dl5CCM0K670JpoYijL5
rAUnfvvcozjhqKpgwalGDsIZ+pDNTL2BjREZulU6mTEiO9iAxLQMZnR0LXRlc3Qt
a2V5iQBOBBARAgAOBQJEtAYrBAsDAQICGQEACgkQ7PgWEnFENBZ0NACgl4QXiLg5
3ziImum1iWQDTjSFBA8AoI9A3rEbeeaqUAHWQMn7x2wtofPiuQMNB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CRLQGLwAKCRDs+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;
0
Comment
Question by:builder110697
  • 3
6 Comments
 
LVL 17

Expert Comment

by:mjcoyne
ID: 17137704
I don't have the Crypt::OpenPGP module installed, so I can't run your code.  What problems are there with the code you posted? Do you get errors?
0
 
LVL 1

Author Comment

by:builder110697
ID: 17138834
Unfortunately it doesn't help too much.

<code>
[builder@engproto crypt]$ perl crypt
ERROR: Need something to encrypt with
[builder@engproto crypt]$
</code>
0
 
LVL 1

Author Comment

by:builder110697
ID: 17138844
Sorry about the "<code>" tags - wrong website ;-)
0
 
LVL 1

Author Comment

by:builder110697
ID: 17154306
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
FrbO0cUrJrit/6NIyzDBCOMEANgBXtHtdrdtmwE35HcFO2sdywvvIOE+L1Wouwku
ewK6/bpLGa1LnYu7T7xO+CziYqjp9SIGiSPmZjQQPXfBrEtCvLMCTC/mdylOP6Sh
hrnLWgPgAV5d7wpR1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88OLeSoF3kE
58hMm9x8xZJOnQeTe4gvX2uDz+F6daxC7xBd73axc2KS9mnBqj9Xw2qLm438TuKC
V7ioOXxzeSVioDNsnh+PLYhEBPFCgsjEqKu0TXWDVkkzzxLO11Uq6VTYt/ve1cVz
mq2vLN3km1kZoumXHYROV9mg2Zr7Y6604r+OAVHxNgJz5X6OB36bjQgpWDPuNWmk
uh1PrZAT6WB2LJDaSZk6y3EQfGWvnZ5OqgqhIVl2HlyMGU4E8C8iTkuCStpndMFw
ZchMKR7UbTswzI7m4t4RPLvpaF8YxmAJy9UzZEK3Ycnq5oNsgicKqgM2GVpky1GO
oAtMaIkARgQYEQIABgUCRLQGLwAKCRDs+BYScUQ0FoObAKD1vGbfNCeu/CVvwsTr
9mz6nUmeTgCeNO9P7OkUuySN6D2YTYbKvgIAEes=
=+ksY
-----END PGP PUBLIC KEY BLOCK-----^ );

my $datafile = "TESTING_MerlintoMerlin-op071106_QAOPT1.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, "> $datafile.pgp" )
  or die "Could not open file for encrypted data - $!";
print OUTFILE $ct;
close OUTFILE;
0
 
LVL 1

Accepted Solution

by:
GhostMod earned 0 total points
ID: 17185271
Closed, 250 points refunded.

GhostMod
Community Support Moderator
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now