Last active
January 1, 2016 20:39
-
-
Save pixeloution/8198313 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 | |
public function encrypt( $plaintext ) | |
{ | |
$iv = mcrypt_create_iv( $this->iv_size, MCRYPT_RAND ); | |
$encrypted = mcrypt_encrypt( MCRYPT_RIJNDAEL_128, $this->key, $plaintext, MCRYPT_MODE_CBC, $iv ); | |
return base64_encode( $iv . $encrypted ); | |
} | |
/** | |
* decrypts a string. assumes the intialization vector is prepended to | |
* the ciphertext | |
* | |
* @param [type] $cipher [description] | |
* | |
* @return mixed | |
* returns a decoded string, or false | |
*/ | |
public function decrypt( $cipher ) | |
{ | |
$cipher = base64_decode( $cipher ); | |
$iv = substr( $cipher, 0, $this->iv_size ); | |
$cipher = substr( $cipher, $this->iv_size ); | |
return mcrypt_decrypt( MCRYPT_RIJNDAEL_128, $this->key, $cipher, MCRYPT_MODE_CBC, $iv ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment