W
W
wolf-98302016-06-07 20:52:24
PHP
wolf-9830, 2016-06-07 20:52:24

Why is the public key not valid?

I generated the key with the openssl.exe program:

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyfFQSZEUlce53iq8CqKq
tRFKkmtn5rrgGk3G/L27nH1tmXxg8egSPJavO+OoS61V7ALBbfffc2srVI/HXBAu
sK1bLLVSQRp3qIvsa/Mqx6vWrujRsCg5Sy5nEaxWEJa6ngRXl3k3LcAG7pOXIr0A
DXlZ3ySdLiNSVX7Ny/0fTv8aqUbSQBweJ16YFTVqer2PEV+xN28Qxd0qhHr32r/I
VSqGtV6md68qhDiJr33in4bvQLtXmp/20N1ACfpU5CyPXMa+QfD2DyyQseHg6TLT
RugZzAW0EgGNUZz/54956U1IjRmpOjSEKusKGheiG4bkfbhlDOPrSlYDgw6CqSKF
UwIDAQAB
-----END PUBLIC KEY-----

Now in php code I am trying to encrypt a string, but it displays an error with the following content:
openssl_public_encrypt(): key parameter is not a valid public key

Here is the code:
$encrypted = '';
      $data = "Hello world";
      $pk  = openssl_get_publickey("../public.pem");
      openssl_public_encrypt($data, $encrypted, $pk);
      echo chunk_split(base64_encode($encrypted));

The path to the key is correct, why is it not valid? How to fix?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lubezniy, 2016-06-07
@lubezniy

And what is the value of the $pk variable before encryption?
And yes. Look closely at php.net/manual/en/function.openssl-pkey-get-public.php - what exactly needs to be substituted into the openssl_get_publickey argument.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question