Last active
February 22, 2026 21:02
-
-
Save chronosceptor/11fffec0d429a45a729257c62f1aa860 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /** | |
| * Encrypt and decrypt | |
| * | |
| * @author Nazmul Ahsan <n.mukto@gmail.com> | |
| * @link http://nazmulahsan.me/simple-two-way-function-encrypt-decrypt-string/ | |
| * | |
| * @param string $string string to be encrypted/decrypted | |
| * @param string $action what to do with this? e for encrypt, d for decrypt | |
| */ | |
| function my_simple_crypt( $string, $action = 'e' ) { | |
| // you may change these values to your own | |
| $secret_key = 'my_simple_secret_key'; | |
| $secret_iv = 'my_simple_secret_iv'; | |
| $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; | |
| } | |
| // To encrypt | |
| $encrypted = my_simple_crypt( 'Hello World!', 'e' ); | |
| // To decrypt | |
| $decrypted = my_simple_crypt( 'RTlOMytOZStXdjdHbDZtamNDWFpGdz09', 'd' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment