Actually, you're referring to symmetric keys. I'm talking about asymmetric keys where the passphrase is embedded in the private key.

I've since managed to get it working properly, although the ciphertext has to be directed into a file, and that file read back in to decrypt. I expect this is due to some incompatibility (or my ignorance of) the formatting types between the terminal and Perl's handling of scalars. I don't think this will be a problem once I output the data to a db table.

For archival sakes, here are the scripts I've used for testing of encrypt() and decrypt():
#!/usr/bin/perl # gpg.pl use Crypt::OpenPGP; my $string = $ARGV[0]; my $pgp = Crypt::OpenPGP->new; my $ciphertext = $pgp->encrypt( Data => $string, Recipients => 'Test User', Armour => 1, ); open(OUT, ">testfile"); print OUT $ciphertext, "\n"; close(OUT);
------------------------
#!/usr/bin/perl # gupg.pl use Crypt::OpenPGP; my $pgp = Crypt::OpenPGP->new; my ($plaintext) = $pgp->decrypt( Filename => 'testfile', Passphrase => 'password', ); die "Decryption failed: ", $pgp->errstr unless $plaintext; print $plaintext, "\n";

Update:
I've managed to test and verify that writes/reads to database also work. Here is the updated code using Data to read in the ciphertext:
#!/usr/bin/perl # gpg.pl use Crypt::OpenPGP; use DBI; my $string = $ARGV[0]; my $dbh = DBI->connect("DBI:mysql:pgpdb:localhost","user","password"); my $insert_stmt = 'insert into pgptable (card) values (?)'; my $sth = $dbh->prepare($insert_stmt); my $pgp = Crypt::OpenPGP->new; my $ciphertext = $pgp->encrypt( Data => $string, Recipients => 'Test User', Armour => 1, ); $sth->execute($ciphertext) || die $dbh->stderr;
-------------------------
#!/usr/bin/perl # gupg.pl use Crypt::OpenPGP; use DBI; my $dbh = DBI->connect("DBI:mysql:pgpdb:localhost","user","password"); my $select_query = 'select card from pgptable where id=?'; my $sth = $dbh->prepare($select_query); my $pgp = Crypt::OpenPGP->new; $sth->execute('1') || die $dbh->stderr; my $data = ($sth->fetchrow_hashref)->{'card'}; my ($plaintext) = $pgp->decrypt( Data => $data, Passphrase => 'passphrase', ); die "Decryption failed: ", $pgp->errstr unless $plaintext; print $plaintext, "\n";

In reply to Re: Re: Using Asymmetric keys for Encryption by fuzzyping
in thread Using Asymmetric keys for Encryption by fuzzyping

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.