Skip to content
Advertisement

openssl_verify(): supplied key param cannot be coerced into a public key

I have converted a test_priv.ppk file to a test_priv.pem file using below:

  1. Start PuTTYgen. For Actions, choose Load, and then navigate to your .ppk file.
  2. Choose the .ppk file, and then choose Open.
  3. From the menu at the top of the PuTTY Key Generator, choose Conversions, Export OpenSSH Key. Note: If you didn’t enter a passphrase, you receive a PuTTYgen warning. Choose Yes.
  4. Name the file and add the .pem extension.
  5. Choose Save.
JavaScript

And generated public key test_pub From Actions Save Public Key button

JavaScript

However, while using PHP-JWT to encode and decode the payload.

JavaScript

What could be causing this error?

Advertisement

Answer

You are using a RSA Privat Key and a RSA Public Key that are in the encoding “PKCS1” which is not usable in PHP OpenSSL:

JavaScript

see information on PHP’s OpenSSL man page: https://www.php.net/manual/en/function.openssl-pkey-get-public.php#101513

To use these keys in your program you need to convert them to “PKCS8” encoded Private/Public Keys that will start with

JavaScript

For conversion you can use online services (but only for a Public Key, never ever for a Private Key).

If you want to do it local I recommend the OpenSSL command line tool (yes, it works with these “traditional” PKCS1 keys …). Just use this command lines:

JavaScript

and you receive these keys (converted from your demo keys in your question):

JavaScript

With these keys the verification (just the Public Key is needed) should work.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement