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}
1function encryptor($action, $string) {
2 $output = FALSE;
3 $encrypt_method = "AES-256-CBC";
4 $secret_key = 'SecretKeyWord';
5 $secret_iv = 'SecretIV@123GKrQp';
6 // hash
7 $key = hash('sha256', $secret_key);
8 // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
9 $iv = substr(hash('sha256', $secret_iv), 0, 16);
10 //do the encryption given text/string/number
11 if ($action == 'encrypt') {
12 $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
13 $output = base64_encode($output);
14 } elseif ($action == 'decrypt') {
15 //decrypt the given text/string/number
16 $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
17 }
18 return $output;
19}
20
21function encrypt($data) {
22 return urlencode(self::encryptor('encrypt', self::sanitize($data)));
23}
24
25function decrypt($data) {
26 return self::encryptor('decrypt', urldecode(self::sanitize($data)));
27}
28// Now you can just call encrypt($string) or decrypt($string)
29?>
1function encrypt($plaintext, $password) {
2 $method = "AES-256-CBC";
3 $key = hash('sha256', $password, true);
4 $iv = openssl_random_pseudo_bytes(16);
5
6 $ciphertext = openssl_encrypt($plaintext, $method, $key, OPENSSL_RAW_DATA, $iv);
7 $hash = hash_hmac('sha256', $ciphertext . $iv, $key, true);
8
9 return $iv . $hash . $ciphertext;
10}
11
12function decrypt($ivHashCiphertext, $password) {
13 $method = "AES-256-CBC";
14 $iv = substr($ivHashCiphertext, 0, 16);
15 $hash = substr($ivHashCiphertext, 16, 32);
16 $ciphertext = substr($ivHashCiphertext, 48);
17 $key = hash('sha256', $password, true);
18
19 if (!hash_equals(hash_hmac('sha256', $ciphertext . $iv, $key, true), $hash)) return null;
20
21 return openssl_decrypt($ciphertext, $method, $key, OPENSSL_RAW_DATA, $iv);
22}
23
24//Example usage:
25$encrypted = encrypt('Plaintext string.', 'password'); // this yields a binary string
26
27echo decrypt($encrypted, 'password');
28// decrypt($encrypted, 'wrong password') === null
1//Key
2$key = 'SuperSecretKey';
3
4//To Encrypt:
5$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, 'I want to encrypt this', MCRYPT_MODE_ECB);
6
7//To Decrypt:
8$decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_ECB);
9