title: Decrypting with openssl_private_decrypt wont work because text is storing apostrophes
body: <p>I store sensitive information in a mysql database in 'text' field format which is encrypted using openssl_public encrypt and descrypted using openssl_private_decrypt.</p>
  
  <p>The issue I face however is that when trying to use my php script to 'decode' the text, the returned value has several apostrophes and quotations within it, therefore making the decryption impossible. Any ideas how to fix this?</p>
  
  <p>Note: I initially tried to store my information in field type 'varbinary' but the decryption failed every time doing so.</p>
  
  <p>After reviewing some more, I think perhaps php and mysql are having a hard time processing all the information (~800 records) because sometimes fields are left blank, othertimes not. If I store one by one, it works with zero issues but when trying to run through the foreach loop it almost always causes errors. Any ideas? Script to encode is below:</p>
  
  <pre><code>&lt;?php
  
  $publickey = file_get_contents("certificate.pem");
  
  function encrypt($text) 
      { 
          return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, SALT, $text, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)))); 
      } 
  
  
      try {
          $db = new PDO('mysql:host=localhost;dbname=DBNAME', 'USER', 'PW');
          $db-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
          $db-&gt;beginTransaction();
  
          $start = microtime(true);
  
          $stmt = $db-&gt;prepare("SELECT ID, ITEM1_old, ITEM2_old  FROM tablename");
  
          $stmt-&gt;execute();
  
          $rows = $stmt-&gt;fetchAll();
  
          foreach($rows as $row) {
  
              $id = $row['0'];
              $item1 = $row['1'];
              $item2 = $row['2'];
  
              define('SALT', $id);
  
              $item1_enc = encrypt($item1);
              $item2_enc = encrypt($item2);
  
              openssl_public_encrypt($item1_enc, $item1_ssl_enc, $publickey);
              openssl_public_encrypt($item2_enc, $item2_ssl_enc, $publickey);
  
              $stmt2 = $db-&gt;prepare("UPDATE tablename SET ITEM1_new=?, ITEM2_new=? WHERE ID=?");
              $stmt2-&gt;execute(array($item1_ssl_enc, $item2_ssl_enc, $id));
  
          }   
  
          $db-&gt;commit();
          $db-&gt;NULL;
  
          $elapsed = microtime(true) - $start;
          echo "Finished.&lt;br /&gt;Elapsed time: ".$elapsed;
      }
  
      catch (PDOException $e)
      {
          $db-&gt;rollback();
          echo "There was a system error.".$e-&gt;getMessage();          
      }
  ?&gt;
  </code></pre>