05/04/2018 - PHP
If you want to encrypt and decrypt a string, you can use example below. The encrypted data will always be dynamic so the result will always be different for given string. Unlike my older post for the same subject, this example does not use mcrypt_get_key_size
function which is deprecated as of PHP 7.1.0 version so follow this example instead of the older one.
declare(strict_types=1);
namespace AppBundle\Util;
class Encryptor
{
private $secret;
private $cipherMethod;
private $separator;
private $ivLength;
public function __construct(
string $secret = 'MyApplicationSecretKey',
string $cipherMethod = 'AES-256-CBC',
string $separator = '::'
) {
$this->secret = $secret;
$this->cipherMethod = $cipherMethod;
$this->separator = $separator;
$this->ivLength = openssl_cipher_iv_length($cipherMethod);
}
public function encrypt(string $data): string
{
$decodedKey = base64_decode($this->secret);
$iv = base64_encode(openssl_random_pseudo_bytes($this->ivLength));
$iv = substr($iv, 0, $this->ivLength);
$encryptedData = openssl_encrypt($data, $this->cipherMethod, $decodedKey, 0, $iv);
return base64_encode($encryptedData.$this->separator.$iv);
}
public function decrypt(string $data): string
{
$decodedKey = base64_decode($this->secret);
list($encryptedData, $iv) = explode($this->separator, base64_decode($data), 2);
$iv = substr($iv, 0, $this->ivLength);
return openssl_decrypt($encryptedData, $this->cipherMethod, $decodedKey, 0, $iv);
}
}
$encrytor = new Encryptor();
$string = 'inanzzz_@$#%&*!';
$encrpted1 = $encrytor->encrypt($string);
$encrpted2 = $encrytor->encrypt($string);
echo 'ENCRYPTED 1: '.$encrpted1.PHP_EOL;
echo 'ENCRYPTED 2: '.$encrpted2.PHP_EOL;
$decrpted1 = $encrytor->decrypt($encrpted1);
$decrpted2 = $encrytor->decrypt($encrpted2);
echo 'DECRYPTED 1: '.$decrpted1.PHP_EOL;
echo 'DECRYPTED 2: '.$decrpted2.PHP_EOL;
ENCRYPTED 1: OVVJSzJXVVJQQ0JOdFFZU29qWXVBQT09Ojovd2trNDBTUnUyejRUZGNY
ENCRYPTED 2: WjVVb2VGVW1ISFJkY3o1aUVpaktPZz09OjpBOG1kOWxjUjg1ZzlXU1NM
DECRYPTED 1: inanzzz_@$#%&*!
DECRYPTED 2: inanzzz_@$#%&*!