title: PHP OpenSSL Public Key Encryption With String Public Key
body: <p>I have a public key</p>
  
  <pre><code>-----BEGIN PUBLIC KEY-----
  MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwKMDEDjbP5v/9kcvpQKf
  IG3nU5Yid/tUNIeXBSDlxqhTEOKs8iQHXk0T17C4g7KHmrT2hxUomaAa2wwbfL+Z
  4ppqvZZ4cu7CO6jaA0HyoBCU96siSuE0mPt8kU/PRA9+nAwu9lu9oYZUiVVJ3D4f
  o2bc+jWWL4GGY+PdSlz81ZW5cW/LOmNs9D0jJIxbwNocHxGgJ+xTZ3JKp6AO4MvL
  zXyipXu562N8wVc7UIgYYnvr63zFU8vzRL180X5x5MiJbjTYbfLH3z7qINPMZZLv
  A5vzJ0HX3J8rG96tmFuUzopCnvf+WVLvaS2T5uMxieK9dfA32CFQp4i3cj39c2b7
  NwIDAQAB
  -----END PUBLIC KEY-----
  </code></pre>
  
  <p>stored as a base64 encoded string (in MySQL, set as $row['public_key'])</p>
  
  <pre><code>LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUF3S01ERURqYlA1di85a2N2cFFLZgpJRzNuVTVZaWQvdFVOSWVYQlNEbHhxaFRFT0tzOGlRSFhrMFQxN0M0ZzdLSG1yVDJoeFVvbWFBYTJ3d2JmTCtaCjRwcHF2Wlo0Y3U3Q082amFBMEh5b0JDVTk2c2lTdUUwbVB0OGtVL1BSQTkrbkF3dTlsdTlvWVpVaVZWSjNENGYKbzJiYytqV1dMNEdHWStQZFNsejgxWlc1Y1cvTE9tTnM5RDBqSkl4YndOb2NIeEdnSit4VFozSktwNkFPNE12TAp6WHlpcFh1NTYyTjh3VmM3VUlnWVludnI2M3pGVTh2elJMMTgwWDV4NU1pSmJqVFliZkxIM3o3cUlOUE1aWkx2CkE1dnpKMEhYM0o4ckc5NnRtRnVVem9wQ252ZitXVkx2YVMyVDV1TXhpZUs5ZGZBMzJDRlFwNGkzY2ozOWMyYjcKTndJREFRQUIKLS0tLS1FTkQgUFVCTElDIEtFWS0tLS0tCg== 
  </code></pre>
  
  <p>in PHP. I'm trying to use this string to encrypt string data, but it just fails without telling me why when I use the following code:</p>
  
  <pre><code>$success = openssl_public_encrypt($data, $encrypted, base64_decode($row['public_key']));
  </code></pre>
  
  <p>When the above is run, <code>$success</code> is always false and <code>$encrypted</code> is blank. I've tried running the public key through <code>openssl_pkey_get_public()</code> and sending the resource to <code>openssl_public_encrypt()</code> with no luck either. Writing to a file is not an option, nor is storing the private key. </p>
  
  <p>Does anyone know how to get this to work? (or does it work for everyone else and it's just a weird server issue?)</p>
  
  <p><strong>EDIT</strong>: Since there seems to be some confusion about my issue, here is the testing code I'm using and its output. (Note: I've removed the base64 encoding, just to see if that made any difference)</p>
  
  <pre><code>$pubkey = openssl_get_publickey($row['public_key']);
  $publicKey = openssl_pkey_get_details($res);
  $encrypted = null;
  $success = openssl_public_encrypt($data, $encrypted, $pubkey);
  print "\npubkey1 " . $row['public_key'];
  print "\npubkey2 " . $pubkey;
  print "\npubkey3 " . $publicKey;
  print "\npubkey4 " . $publicKey["key"];
  print "\nencryption " . ($success ? 'true' : 'false') . ' "' . $encrypted . '"';
  $success = openssl_public_encrypt($data, $encrypted, $row['public_key']);
  print "\nencryption2 " . ($success ? 'true' : 'false') . ' "' . $encrypted . '"';   
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>pubkey1 -----BEGIN PUBLIC KEY-----
  MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA89FgfElm36q7iAf8frqa
  o58naoROsAljaFbmztfnqlmzXfyijK5CNJFdkUCgsu2zGxN7UlGiBOassTd4ijWm
  1rz6/ad9fGXplfMGxZxyPCz31VreSWXmTG/PeSIYs1Co+dibV3imYt5jTxfLs7BZ
  WsT8nuLxGPw/o/gyKut0Ru+jXI2GgT4s3SylXinn/IbIA497SohqYA7/ViQnBwSL
  ZKUysOx2QgBmc9m0viRqDSKNUtDw7+L7bjhlwgZUGr6fxfTuNj9PWo97aPSE74CD
  owYYl2ToTboKSjZUszeNwQKpUnlHY/DBkwmYUJ7SAYDY70VNooadN5dZ4ehjdaka
  6QIDAQAB
  -----END PUBLIC KEY-----
  
  pubkey2 Resource id #50
  pubkey3 
  pubkey4 
  encryption false ""
  encryption2 false ""
  </code></pre>