willjones has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to decrypt an Open PGP message string that was created with the haneWIN Javascript Open PGP encrypter. (http://www.hanewin.net/encrypt)

I have a file at "$BASEDIR/private.pgp" with my private key and a file at "$BASEDIR/public.pgp" with my public key. (I am running this in a CGI page.)

Here's the code I'm trying...
use Crypt::OpenPGP; use Crypt::OpenPGP::KeyRing; my $PGPEncryptedMessage = "-----BEGIN PGP MESSAGE-----Version: haneWIN + JavascriptPG v2.0hIwDAAAAAAAAAAABA/4oJfsBR88yts6eY22RJDwEFyhxi2KWIJx +6BR8fufIDN4j3H79sCTN3UHDYoCV5zluSyhdSmM8beF1+hQ7PvSsonDOXrGfovIb9/cIt +QQg1CrgI63Xje85qWW3IUpebnt4UlPnXx1sxK46GtJIEckYppWbXubCunAdFhhCyiq4ZB +qQnZ9EAojfFrI8zkVIC/gq7cMZsu1wgn7/lcUvFewUNWWrU8ykAiC5t=R2eG-----END +PGP MESSAGE-----"; my $arg; my $secring = Crypt::OpenPGP::KeyRing->new(Filename => "$BASEDIR/priva +te.pgp") or die "Error: ".Crypt::OpenPGP::KeyRing->errstr; my $pubring = Crypt::OpenPGP::KeyRing->new(Filename => "$BASEDIR/publi +c.pgp") or die "Error: ".Crypt::OpenPGP::KeyRing->errstr; $arg{SecRing} = $secring; $arg{PubRing} = $pubring; my $pgp = Crypt::OpenPGP->new(%arg); my $message = $pgp->decrypt(Data => $PGPEncryptedMessage) or die "Erro +r: ".$pgp->errstr; print $cgi->header(); print "The secret message is: ".$message;
When I run this it errors out on the $pgp->decrypt call and I get the following error message back...
"Error: Reading data packets failed: Unrecognizable armour"

Any ideas what I am doing wrong here?

Thanks

Replies are listed 'Best First'.
Re: Decrypting a message with Crypt::OpenPGP
by mscharrer (Hermit) on Apr 23, 2008 at 21:25 UTC
    The enc. message lacks the needed newlines. There must be a line break after the ---BEGIN ... --- line and two after the version line. Then one every 65 characters until the --- END .. --- line which has to start with one.

    Change the my $PGPEncryptedMessage line to:

    my $PGPEncryptedMessage = <<'EOM'; -----BEGIN PGP MESSAGE----- Version: haneWIN JavascriptPG v2.0 hIwDAAAAAAAAAAABA/4oJfsBR88yts6eY22RJDwEFyhxi2KWIJx6BR8fufIDN4j3 H79sCTN3UHDYoCV5zluSyhdSmM8beF1+hQ7PvSsonDOXrGfovIb9/cItQQg1CrgI 63Xje85qWW3IUpebnt4UlPnXx1sxK46GtJIEckYppWbXubCunAdFhhCyiq4ZBqQn Z9EAojfFrI8zkVIC/gq7cMZsu1wgn7/lcUvFewUNWWrU8ykAiC5t=R2eG -----END PGP MESSAGE----- EOM
    It's also better to use single quotes for such texts to avoid accidental expansion. Ok, ` $ @ & might not be valid anyway in PGP messages.

    PS: my $arg; should be my %arg;. Just add

    use strict; use warnings;
    at the very start of you script.
      Great Thanks. I put in the changes you indicated and now I am getting a different error.
      It says... "Error: Can't find a secret key to decrypt message"
      The private.pgp and public.pgp files are binary and have something like G4... in them
      I think it is finding these files okay because if I change the $secring assignment line to look for private1.pgp which doesn't exist it fails saying it can't find that file on that line.
      Am I missing something?
      Any ideas about what is wrong now?

      Thanks
        I don't have experience with this particular module only with PGP/GPG in general, so I saw this error. I know however that there are different binary formats for RSA/DSA keys, and PGP/GPG keys are such. E.g. I had to convert an binary RSA key generated under Windows once to a different format to use it under Linux.

        Just try to import the binary key with GPG or the PGP freeware and then to export it again in ASCII format.