1function encrypt_decrypt($string, $action = 'encrypt')
2{
3 $encrypt_method = "AES-256-CBC";
4 $secret_key = 'AA74CDCC2BBRT935136HH7B63C27'; // user define private key
5 $secret_iv = '5fgf5HJ5g27'; // user define secret key
6 $key = hash('sha256', $secret_key);
7 $iv = substr(hash('sha256', $secret_iv), 0, 16); // sha256 is hash_hmac_algo
8 if ($action == 'encrypt') {
9 $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
10 $output = base64_encode($output);
11 } else if ($action == 'decrypt') {
12 $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
13 }
14 return $output;
15}
16
17echo "Your Encrypted password is = ". $pwd = encrypt_decrypt('spaceo', 'encrypt');
18echo "Your Decrypted password is = ". encrypt_decrypt($pwd, 'decrypt');
19
1function encryptPass($password) {
2 $sSalt = '20adeb83e85f03cfc84d0fb7e5f4d290';
3 $sSalt = substr(hash('sha256', $sSalt, true), 0, 32);
4 $method = 'aes-256-cbc';
5
6 $iv = chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0);
7
8 $encrypted = base64_encode(openssl_encrypt($password, $method, $sSalt, OPENSSL_RAW_DATA, $iv));
9 return $encrypted;
10}
11
12function decryptPass($password) {
13 $sSalt = '20adeb83e85f03cfc84d0fb7e5f4d290';
14 $sSalt = substr(hash('sha256', $sSalt, true), 0, 32);
15 $method = 'aes-256-cbc';
16
17 $iv = chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0);
18
19 $decrypted = openssl_decrypt(base64_decode($password), $method, $sSalt, OPENSSL_RAW_DATA, $iv);
20 return $decrypted;
21}