<?php
  error_reporting(E_ALL);

  $data= str_repeat('-', (int)$argv[1]);
  $cert= 'my.cert';

  printf('---> Get public key from %s: ', $cert);
  $pkey= openssl_get_publickey(file_get_contents($cert));
  var_dump($pkey);
  
  printf('---> Encrypt %d bytes of data using public key: ', strlen($data));
  $r= openssl_public_encrypt($data, $crypted, $pkey);
  var_dump($r);

  printf('---> Freeing public key: ');
  $f= openssl_free_key($pkey);
  var_dump($f);
  
  if (FALSE === $r) {
    die("***  Encryption failed!\n");
  }

  printf('---> Encoding %d bytes of encrypted data: ', strlen($crypted));
  $encoded= base64_encode($crypted);
  var_dump($encoded);
?>
