title: data encryption/decryption for PHP 7.1 and higher
body: <p>I currently have a PHP application that uses data-at-rest encryption to store sensitive information onto a database that I can later decrypt for use in my application. </p>
  
  <p>Here is the current code I'm using.  I kno...i kno...it's not very secure at all but it works fine for what I using it for.  I'm not storing credit card numbers, social security number, or anything like that.  Mainly just names and addresses.</p>
  
  <pre><code>define('CRYPTO_KEY', 'some-key');
  
  function decrypt($val){
      $val = urlencode($val);
      $data = base64_decode(urldecode($val));
      $iv = substr($data, 0, mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC));
      $val = rtrim(
          mcrypt_decrypt(
              MCRYPT_RIJNDAEL_128,
              hash('sha256', CRYPTO_KEY, true),
              substr($data, mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC)),
              MCRYPT_MODE_CBC,
              $iv
          ),
      "\0"
      );
      return $val;
  }
  
  function enc($val){
      $iv = mcrypt_create_iv(
          mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC),
          MCRYPT_DEV_URANDOM
      );
  
      $val = urlencode(base64_encode(
          $iv .
          mcrypt_encrypt(
              MCRYPT_RIJNDAEL_128,
              hash('sha256', CRYPTO_KEY, true),
              $selection,
              MCRYPT_MODE_CBC,
              $iv
          )
      )); 
      return $val;
  }
  </code></pre>
  
  <p>The problem I'm running into is that I just upgraded from PHP 5.5 to PHP 7.1.  Now in PHP 7.1 my encrypt/decrypt function throws a ton of warnings out stating that the following function are deprecated:</p>
  
  <ul>
  <li>mcrypt_get_iv_size()</li>
  <li>mcrypt_create_iv() </li>
  <li>mcrypt_encrypt() </li>
  <li>mcrypt_decrypt()</li>
  </ul>
  
  <p>I was doing some research and found that the new recommendations are to use libraries like libsodium or OpenSSL found <a href="https://paragonie.com/white-paper/2015-secure-php-data-encryption" rel="nofollow noreferrer" title="paragonie.com white paper">HERE</a></p>
  
  <p>I looked into both but the documentation is way too technical for me to understand.  I can't even figure out how to install libsodium, let alone use it.</p>
  
  <p>My question is, does anyone have a decent encrypt/decrypt function that is compatible with PHP 7.1 or higher they'd be willing to share?  </p>
  
  <p>Better yet, would anyone be willing to provide instructions on how to use libsodium or OpenSSL for data encryption/decryption?   </p>
  
  <p>FYI - I'm currently using a shared hosting platform with cPanle.  I have composer installed and understand how to install packages through it.  I have never used PECL or PEAR  </p>