Created
July 13, 2015 04:52
-
-
Save karmiphuc/8f94f3042fe2386b4f17 to your computer and use it in GitHub Desktop.
PHP Encryption/Decryption done right (PHP 5.3+)
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
| <?php | |
| /** | |
| * Returns an encrypted & utf8-encoded | |
| */ | |
| function encrypt($pure_string) { | |
| $encryption_key = 'random_string_here_as_private_key'; | |
| $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB); | |
| $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); | |
| $encrypted_string = mcrypt_encrypt(MCRYPT_BLOWFISH, $encryption_key, utf8_encode($pure_string), MCRYPT_MODE_ECB, $iv); | |
| return $encrypted_string; | |
| } | |
| /** | |
| * Returns decrypted original string | |
| */ | |
| function decrypt($encrypted_string) { | |
| $encryption_key = 'random_string_here_as_private_key'; | |
| $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB); | |
| $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); | |
| $decrypted_string = mcrypt_decrypt(MCRYPT_BLOWFISH, $encryption_key, $encrypted_string, MCRYPT_MODE_ECB, $iv); | |
| return $decrypted_string; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment