Openssl_verify (): the specified key parameter cannot be forced into the public key for the .pem file

Currently trying to read the .pem public key in order to verify it through openssl .

 /** * Check whether the signed message sent back by the server is * correct or not. */ function check($str, $MAC) { $fp = fopen( dirname(__FILE__) . '/rsa_public_key.pem', 'r' ); $cert = fread($fp, 8192); fclose($fp); $pubkeyid = openssl_get_publickey($cert); return openssl_verify($str, $MAC, $pubkeyid); } 

With that said, after running my script, I get this error:

openssl_verify(): supplied key param cannot be coerced into a public key in some/path at line X

I originally wrote this function to accept .cer certificates. This explains the difference between all of these different key formats . As far as I understand, .pem similar to .cer , however I couldn’t understand all my life how to allow my script to read my .pem file.

My question is: what do I need to make my function read this public key?

EDIT: After some Googling, I tried using file_get_contents() for a specific path, but I would get the same error.

What can cause this error?

+6
source share
2 answers

After opening this .pem file .pem everything was on one line. Each line seems to require a length of 64 characters, so I made sure that each line has 64 lines and is successfully parsed. Has nothing to do with .cer .

+4
source

In addition, the lines -----BEGIN PUBLIC KEY----- and -----END PUBLIC KEY----- must contain exactly five dashes on each side. No more no less.

At the end of the last line, there may or may not be a new line.

Windows trailing lines (CR / LF) are allowed even in * nix-hosted PHP.

+2
source

Source: https://habr.com/ru/post/976827/


All Articles