Been trying to get a CGI to decrypt an encoded, armored string sent via a post. If the string is saved to the file (say) 'file.txt' then "gpg --decrypt file.txt" from the bash shell will successfully decrypt the file, once I use the passphase 'abc123!' for the passphrase to the secret key.

However, if I try to decode this in the CGI script itself using Crypt::OpenPGP, I cannot get it to work. I've modified the code until the cows come home and most of the time I get the same error message "Symkey decrypt failed: Invalid secret key ID". Here's the relevant code snippet:

use Crypt::OpenPGP; use CGI; my $pgp = Crypt::OpenPGP->new(Compat => 'GnuPG'); my $cur=CGI->new(); my $phrase=$cur->param('phrase'); my ($plaintext,$valid,$sig)=$pgp->decrypt(Data => $phrase, Passphrase +=> "abc123!"); print "[message: $plaintext<br>valid: $valid<br>signature: $sig<br>err +or: ".$pgp->errstr."]<br><br>\n";

The last print statement returns the following:

message:
valid:
signature:
error: Symkey decrypt failed: Invalid secret key ID

And yet, if I dump the contents of the variable $phrase to the file file.txt and do gpg --decrypt file.txt from bash with passphrase abc123! to unlock the secret key it will successfully decode.


In reply to Need help decoding string in Crypt::OpenPGP by jaf0faj

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.