Skip to content

Instantly share code, notes, and snippets.

@pixeloution
Last active January 1, 2016 20:39
Show Gist options
  • Save pixeloution/8198313 to your computer and use it in GitHub Desktop.
Save pixeloution/8198313 to your computer and use it in GitHub Desktop.
<?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