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
180 stars 69 forks source link

Error: Ciphertext representative out of range #129

Open soft17 opened 1 year ago

soft17 commented 1 year ago

can please someone help me with decripting message error? it throw "Ciphertext representative out of range"

Fatal error:  Uncaught OutOfRangeException: Ciphertext representative out of range in /pgp/vendor/phpseclib/phpseclib/phpseclib/Crypt/RSA/PrivateKey.php:68
Stack trace:
#0 /pgp/vendor/phpseclib/phpseclib/phpseclib/Crypt/RSA/PrivateKey.php(320): phpseclib3\Crypt\RSA\PrivateKey->rsadp()
#1 /pgp/vendor/phpseclib/phpseclib/phpseclib/Crypt/RSA/PrivateKey.php(441): phpseclib3\Crypt\RSA\PrivateKey->rsaes_pkcs1_v1_5_decrypt()
#2 /pgp/lib/openpgp_crypt_rsa.php(218): phpseclib3\Crypt\RSA\PrivateKey->decrypt()
#3 /pgp/lib/openpgp_crypt_rsa.php(200): OpenPGP_Crypt_RSA::try_decrypt_session()

my code:

$keyEncrypted = OpenPGP_Message::parse(OpenPGP::unarmor(file_get_contents(private.asc'), 'PGP PRIVATE KEY BLOCK'));

// try each secret key packet
foreach($keyEncrypted as $p) {
    if(!($p instanceof OpenPGP_SecretKeyPacket)) continue;
    $key = OpenPGP_Crypt_Symmetric::decryptSecretKey('secret', $p);
    $msg = OpenPGP_Message::parse(OpenPGP::unarmor($msgEncrypted, 'PGP MESSAGE'));
    $decryptor = new OpenPGP_Crypt_RSA($key);
    $decrypted = $decryptor->decrypt($msg); // if i call this get that error
}

thanks <3

soft17 commented 1 year ago

i think its fixed by replace this line:

if(!($p instanceof OpenPGP_SecretKeyPacket)) continue;

by:

if(!(get_class($p) == 'OpenPGP_SecretKeyPacket')) continue;

there was "OpenPGP_SecretSubkeyPacket" what is also instanceof "OpenPGP_SecretKeyPacket", change skipped that subkey