Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Need help decoding string in Crypt::OpenPGP

by jaf0faj (Novice)
on Mar 10, 2012 at 17:39 UTC ( #958887=perlquestion: print w/ replies, xml ) Need Help??
jaf0faj has asked for the wisdom of the Perl Monks concerning the following question:

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.

Comment on Need help decoding string in Crypt::OpenPGP
Download Code
Re: Need help decoding string in Crypt::OpenPGP
by BrowserUk (Pope) on Mar 10, 2012 at 17:47 UTC

    Have you checked your logs? This looks wrong:

    my ($plaintext,$valid,$sig)=$pgp->decrypt(Data => $phrase, Passphrase ++=> "abc123!");

    You should have seen:

    %s = ( Passphrase +=> 'abc123!' );; [syntax error at (eval 9) line 1, near "+=>"

    There is weirdness in the next line also: $sig<br>err +or: ".


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      Looks like the "+"s are just the result of having cut-n-pasted the code from the original anonymous post, in which it was correct...

        Eliya is correct, that was a cut and paste error from the original anonymous post. Sorry about that. It should have read:

        Passphrase => ...

        I tidied up the original message

Re: Can't decode string in Crypt::OpenPGP
by zwon (Monsignor) on Mar 11, 2012 at 05:51 UTC

    Do you run this cgi script using the same user as when you decrypting with gpg? What happens if you run it without cgi, something like this (not tested!):

    use Crypt::OpenPGP; use File::Slurp; my $pgp = Crypt::OpenPGP->new(Compat => 'GnuPG'); my $phrase=read_file('file.txt'); 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";

      Unfortunately, no difference. Copied your code verbatim into the file 'decodetest', replaced instances of '<br>' with '\n' in the print statement, then ran "perl decodetest". Same results:
      message:
      valid:
      signature:
      error: Symkey decrypt failed: Invalid secret key ID

Re: Need help decoding string in Crypt::OpenPGP
by jaf0faj (Novice) on Mar 17, 2012 at 15:53 UTC

    Update: Curtis Leach had a similar issue in 2009 and sought help on the internet, with no response to his issue. It is the similar to here. As he pointed out, and I confirmed on my machine, there are four possible combinations here and one of the four fails (unfortunately, it is the critical one for my situation and Curtis').

    1. Encrypt with gpg and decrypt with gpg.
      Works fine, as expected
    2. Encrypt with perl using Crypt::OpenPGP and decrypt with gpg.
      Works fine.
    3. Encrypt with perl using Crypt::OpenPGP and decrypt with perl using Crypt::OpenPGP
      Works fine.
    4. Encrypt with gpg and decrypt with perl using Crypt::OpenPGP
      Failure with "Symkey decrypt failed: Invalid secret key ID" message. This is the situation described above

    In fact, for the last combination in my case, the file was encrypted by javascript on another machine. However, if I generated an encrypted file with gpg the same error arises.

      I'm having the same issue...Any solution?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://958887]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (2)
As of 2014-07-31 00:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (242 votes), past polls