singpolyma / openpgp-php

OpenPGP.php is a pure-PHP implementation of the OpenPGP Message Format (RFC 4880).
http://singpolyma.github.io/openpgp-php/
The Unlicense
179 stars 69 forks source link

Cannot decrypt messages encoded with GPG #133

Open bouboot82 opened 1 year ago

bouboot82 commented 1 year ago

Hi,

Every time I try to run the decryption algorithm on messages encoded with gpg (the key pairs have been generated in gpg as well), I get the following errors :

Severity: Warning

Message: Undefined array key "e"

Filename: lib/openpgp_crypt_rsa.php

Line Number: 250

Backtrace:

File: /OpenPGP-PHP/lib/openpgp_crypt_rsa.php Line: 250 Function: _error_handler

...

And

A PHP Error was encountered Severity: Warning

Message: Undefined array key "d"

Filename: lib/openpgp_crypt_rsa.php

Line Number: 251

Backtrace:

File: OpenPGP-PHP/lib/openpgp_crypt_rsa.php Line: 251 Function: _error_handler

The first and the second errors are triggered when using the armored version of the key and the message. The second one only is triggered when using the unarmored versions.

The code used for the ASCII version is :

        $keyEncrypted = OpenPGP_Message::parse(OpenPGP::unarmor(file_get_contents(dirname(__FILE__) . '/test.key'), 'PGP PRIVATE KEY BLOCK'));
        foreach($keyEncrypted as $p) {
            if(!($p instanceof OpenPGP_SecretKeyPacket)) continue;

            $key = OpenPGP_Crypt_Symmetric::decryptSecretKey('Passphrase', $p);

            $msg = OpenPGP_Message::parse(OpenPGP::unarmor(file_get_contents(dirname(__FILE__) . '/testArmored.txt'), 'PGP MESSAGE'));

            $decryptor = new OpenPGP_Crypt_RSA($key);
            $decrypted = $decryptor->decrypt($msg);
        }

For the unarmored version

        $key = OpenPGP_Message::parse(file_get_contents(dirname(__FILE__) . '/test.gpg'));
        $decryptor = new OpenPGP_Crypt_RSA($key);
        $decrypted = $decryptor->decrypt(file_get_contents(dirname(__FILE__) . '/test.asc'));  

The files used for the test are attached.

Thanks in advance Archive.zip