title: Get the key parameter is not a valid public key error in openssl_public_encrypt()
body: <p>$publicKey = "../ssh/public/pub";
      $plaintext = "String to encrypt";</p>
  
  <pre><code>$pubKey = openssl_pkey_get_public($publicKey);
  
  openssl_public_encrypt($plaintext, $encrypted, $pubKey);
  
  echo $encrypted;   //encrypted string
  </code></pre>
  
  <p>above code generating following error</p>
  
  <p>openssl_public_encrypt() [http://php.net/function.openssl-public-encrypt]: key parameter is not a valid public key [APP/controllers/supportservice_controller.php, line 144]</p>
  
  <p>I created the keys with openssl using:</p>
  
  <p>generate a 1024 bit rsa private key, ask for a passphrase to encrypt it and save to file
  openssl genrsa -des3 -out /path/to/privatekey 1024</p>
  
  <p>generate the public key for the private key and save to file</p>
  
  <p>openssl rsa -in /path/to/privatekey -pubout -out /path/to/publickey</p>