Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * https://bhoover.com/using-php-openssl_encrypt-openssl_decrypt-encrypt-decrypt-data/
- * @param $data
- * @param $key
- * @return string
- */
- function encrypt($data, $key)
- {
- //Remove the base64 encoding from our key
- $encryption_key = base64_decode($key);
- // Generate an initialization vector
- $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
- // Encrypt the data using AES 256 encryption in CBC mode using our encryption key and initialization vector.
- $encrypted = openssl_encrypt($data, 'aes-256-cbc', $encryption_key, 0, $iv);
- // The $iv is just as important as the key for decrypting, so save it with our encrypted data using a unique separator (::)
- return base64_encode($encrypted . '::' . $iv);
- }
- echo encrypt('string', 1234);
- echo '<br />';
- function decrypt($data, $key)
- {
- //Remove the base64 encoding from our key
- $encryption_key = base64_decode($key);
- // To decrypt, split the encrypted data from our IV - our unique separator used was "::"
- list($encrypted_data, $iv) = explode('::', base64_decode($data), 2);
- return openssl_decrypt($encrypted_data, 'aes-256-cbc', $encryption_key, 0, $iv);
- }
- echo decrypt('NlZDN2trR1R1K3diaERNa2ROMjZ2dz09OjoRssvy+nlh9tszAHCXW3E8', 1234);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement