-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample2.php
52 lines (39 loc) · 1.26 KB
/
example2.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
/**
* Encrypt and decrypt
*
* @author Karthick <karthi.karthi47@gmail.com>
* @link Coming Soon
*
* @param string $string string to be encrypted/decrypted
* @param string $action what to do with this? e for encrypt, d for decrypt
*/
function encrypt_decrypt( $string, $action = 'e' ) {
// you may change these values to your own
$secret_key = 'karthick@2018@isthehiddenkey#$%';
$secret_iv = '08062018';
$output = false;
$encrypt_method = "AES-256-CBC";
$key = hash( 'sha256', $secret_key );
$iv = substr( hash( 'sha256', $secret_iv ), 0, 16 );
if( $action == 'e' ) {
$output = base64_encode( openssl_encrypt( $string, $encrypt_method, $key, 0, $iv ) );
}
else if( $action == 'd' ){
$output = openssl_decrypt( base64_decode( $string ), $encrypt_method, $key, 0, $iv );
}
return $output;
}
$plain_txt = "Smile Please!";
$encrypted_txt = encrypt_decrypt( $plain_txt, 'e' );
echo "Encrypted Text = " .$encrypted_txt. "\n";
echo '<br>';
$decrypted_txt = encrypt_decrypt( $encrypted_txt, 'd' );
echo "Decrypted Text =" .$decrypted_txt. "\n";
echo '<br>';
if ( $plain_txt === $decrypted_txt )
echo "SUCCESS";
else
echo "FAILED";
echo '<br>';
?>