Created
January 2, 2021 17:42
-
-
Save shekarsiri/da2b1b1de3a61ddff72607dc94db0be9 to your computer and use it in GitHub Desktop.
Encryption and Decrytion for PHP
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 | |
/** | |
* Encrypts a string using Rijndael with a 256-bit (32 character) salt. | |
* | |
* @param string $text The text to encrypt. | |
* @param string $key The encryption key to use. | |
* @param string $iv The optional initialization vector to use. | |
* | |
* @return string Base64 encoded encrypted text. | |
*/ | |
public function encrypt($text, $key, $iv = NULL) { | |
assert(TRUE === is_string(base64_encode($text))); | |
assert(TRUE === is_string(base64_encode($key))); | |
assert(TRUE === is_null($iv) || TRUE === is_string(base64_encode($iv))); | |
$return = $text; | |
if (FALSE === isset($iv)) { | |
$iv = mhash(MHASH_SHA256, $key); | |
} | |
$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_256, '', MCRYPT_MODE_CBC, ''); | |
$key = substr(pad($key, mcrypt_enc_get_key_size($cipher)), 0, mcrypt_enc_get_key_size($cipher)); | |
$iv = substr(pad($iv, mcrypt_enc_get_iv_size($cipher)), 0, mcrypt_enc_get_iv_size($cipher)); | |
if (mcrypt_generic_init($cipher, $key, $iv) !== -1) { | |
$return = mcrypt_generic($cipher, $text); | |
mcrypt_generic_deinit($cipher); | |
$return = base64_encode($return); | |
} | |
return $return; | |
} | |
/** | |
* Decrypts a Base64 encoded string that was encrypted by "encrypt". | |
* | |
* @param string $text The Base64 text to decrypt. | |
* @param string $key The encryption key to use. | |
* @param string $iv The initialization vector to use. | |
* | |
* @return string Decrypted text or binary string. | |
*/ | |
public function decrypt($text, $key, $iv = NULL) { | |
assert(TRUE === is_string($text)); | |
assert(TRUE === is_string(base64_encode($key))); | |
assert(TRUE === is_NULL($iv) || TRUE === is_string(base64_encode($iv))); | |
$return = $text; | |
if (FALSE === isset($iv)) { | |
$iv = mhash(MHASH_SHA256, $key); | |
} | |
$text = base64_decode($text); | |
$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_256, '', MCRYPT_MODE_CBC, ''); | |
$key = substr(pad($key, mcrypt_enc_get_key_size($cipher)), 0, mcrypt_enc_get_key_size($cipher)); | |
$iv = substr(pad($iv, mcrypt_enc_get_iv_size($cipher)), 0, mcrypt_enc_get_iv_size($cipher)); | |
if (mcrypt_generic_init($cipher, $key, $iv) !== -1) { | |
$return = mdecrypt_generic($cipher, $text); | |
mcrypt_generic_deinit($cipher); | |
} | |
return trim($return); | |
} | |
/** | |
* Pads or trims a string to the specified number of characters. | |
* | |
* @param string $text String or binary string to pad. | |
* @param int $length The number of characters to return. | |
* @param string $char Optionally, the character used to pad the string. | |
* | |
* @return string The padded or trimmed string. | |
*/ | |
private function pad($text, $length, $char = ' ') { | |
assert(TRUE === is_string(base64_encode($text))); | |
assert(TRUE === is_int($length)); | |
assert(TRUE === is_string($char)); | |
$return = $text; | |
if (mb_strlen($text) < $length) { | |
$return = mb_str_pad($text, $length, $char); | |
} | |
if (mb_strlen($text) > $length) { | |
$return = mb_substr($text, 0, $length); | |
} | |
return $return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment