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.
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.
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;
}
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:
- mcrypt_get_iv_size()
- mcrypt_create_iv()
- mcrypt_encrypt()
- mcrypt_decrypt()
I was doing some research and found that the new recommendations are to use libraries like libsodium or OpenSSL found HERE
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.
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?
Better yet, would anyone be willing to provide instructions on how to use libsodium or OpenSSL for data encryption/decryption?
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