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 use object of type OpenPGP_LiteralDataPacket as array #99

Closed thevalo closed 3 years ago

thevalo commented 4 years ago

Hello, I'm trying example sign.php with just replacing this line: $wkey = OpenPGP_Message::parse(file_get_contents('sec.asc')); where sec.asc is a secret key, without password-protection, however when opening it this error appears:

Fatal error: Uncaught Error: Cannot use object of type OpenPGP_LiteralDataPacket as array in C:\xampp\htdocs\openpgp-php\lib\openpgp_crypt_rsa.php:25 Stack trace: #0 C:\xampp\htdocs\openpgp-php\examples\sign.php(15): OpenPGP_Crypt_RSA->__construct(Object(OpenPGP_LiteralDataPacket)) #1 {main} thrown in C:\xampp\htdocs\openpgp-php\lib\openpgp_crypt_rsa.php on line 25

Any idea why this would happen?

hallomj commented 3 years ago

Dont use this line

$wkey = $wkey[0];

singpolyma commented 3 years ago

$wkey = OpenPGP_Message::parse(file_get_contents('sec.asc'));

This line is almost certainly wrong, since it seems the .asc extension means it will be an ASCII armored key and not just a binary key. You want this:

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