· 5 years ago · Nov 14, 2019, 06:58 AM
1$someString = 'abcds'
2$secretKey = 'Some-random-key';
3
4$output = false;
5$encryptMethod = 'AES-256-CBC';
6$keyHash = hash( 'sha256', $secretKey );
7$key = substr($keyHash, 0, 32);
8$iv = substr($keyHash, 32, 16);
9
10// Encrypt
11echo base64_encode( openssl_encrypt( $string, $encryptMethod, $key, 0, $iv ) );
12
13// Decrypt
14openssl_decrypt( base64_decode( $string ), $encryptMethod, $key, 0, $iv );
15
16
17 return $output;