Created
June 28, 2018 23:41
-
-
Save Zorono/f058acc6f8f75d4e661243bb3ed997af to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
const OPENSSL_ENCRYPTz = 0; | |
const OPENSSL_DECRYPTz = 1; | |
function OpenSSLEndeCrypt($action = OPENSSL_ENCRYPTz, $string = '', $ende_options = OPENSSL_RAW_DATA, $encrypt_method = "AES-256-CBC") | |
{ | |
$output = NULL; | |
$key = hash("sha256", openssl_random_pseudo_bytes(32), True); | |
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($encrypt_method)); | |
if ($action == OPENSSL_ENCRYPTz) | |
{ | |
$output = ($output['OPENSSL_ENCRYPT'] = base64_encode(openssl_encrypt($string, $encrypt_method, $key, $ende_options, $iv)) && strlen($output['OPENSSL_ENCRYPT']) >= 1 ? $output['OPENSSL_ENCRYPT'] : base64_encode($string)); | |
} | |
else if($action == OPENSSL_DECRYPTz) | |
{ | |
$output = ($output['OPENSSL_DECRYPT'] = openssl_decrypt(base64_decode($string), $encrypt_method, $key, $ende_options, $iv) && strlen($output['OPENSSL_DECRYPT']) >= 1 ? $output['OPENSSL_DECRYPT'] : base64_decode($string)); | |
} | |
return (strlen($output) >= 1 ? $output: 'FAILURE'); | |
} | |
$encrypted_text = OpenSSLEndeCrypt(OPENSSL_ENCRYPTz, 'Advanced OpenSSL/Base64 Ende-Cryptor.'); | |
echo '<br />Your Encrypted Text: '. $encrypted_text. '<br />'; | |
echo 'Your Decrypted Text: '. OpenSSLEndeCrypt(OPENSSL_DECRYPTz, $encrypted_text). '<br />'; | |
/* | |
Expected Output:- | |
* Your Encrypted Text: QWR2YW5jZWQgT3BlblNTTC9CYXNlNjQgRW5kZS1DcnlwdG9yLg== | |
* Your Decrypted Text: Advanced OpenSSL/Base64 Ende-Cryptor. | |
*/ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment