Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

Crypt::OpenPGP without external dependencies

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
3ziImum1iWQDTjSFBA8AoI9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qgM2GVpky1GO
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;
0
builder110697
Asked:
builder110697
  • 3
1 Solution
 
mjcoyneCommented:
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
 
builder110697Author Commented:
Unfortunately it doesn't help too much.

<code>
[builder@engproto crypt]$ perl crypt
ERROR: Need something to encrypt with
[builder@engproto crypt]$
</code>
0
 
builder110697Author Commented:
Sorry about the "<code>" tags - wrong website ;-)
0
 
builder110697Author Commented:
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
hrnLWgPgAV5d7wpR1VUQCugDDZ9s/ExQuQEeD3dHt2dBBco5/+lvdr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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
 
GhostModCommented:
Closed, 250 points refunded.

GhostMod
Community Support Moderator
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now