perlquestion
jaf0faj
<p>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) '<tt>file.txt</tt>' then "<tt>gpg --decrypt file.txt</tt>" from the bash shell will successfully decrypt the file, once I use the passphase '<tt>abc123</tt>!' for the passphrase to the secret key. </p>
<p>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 "<tt>Symkey decrypt failed: Invalid secret key ID</tt>". Here's the relevant code snippet:</p>
<code>
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>error: ".$pgp->errstr."]<br><br>\n";
</code>
<p>The last print statement returns the following:<br><br>
<tt>message:</tt><br>
<tt>valid: </tt><br>
<tt>signature: </tt><br>
<tt>error: Symkey decrypt failed: Invalid secret key ID </tt><br>
</p>
<p>And yet, if I dump the contents of the variable <tt>$phrase</tt> to the file <tt>file.txt</tt> and do <tt>gpg --decrypt file.txt</tt> from bash with passphrase <tt>abc123!</tt> to unlock the secret key it will successfully decode. </p>