Skip to content

Instantly share code, notes, and snippets.

@Robertof
Created August 26, 2012 16:13
Show Gist options
  • Save Robertof/3481370 to your computer and use it in GitHub Desktop.
Save Robertof/3481370 to your computer and use it in GitHub Desktop.
This snippet allows to encrypt and decrypt with mcrypt in PHP. Default algorithm and mode are Rijndael256 and CBC.
<?php
// Author: Robertof
// You are free to use this, but please don't remove my name from the source. ty :3
class EncryptionManager
{
private $algorithm;
private $mode;
private $cipher;
public function __construct ($algorithm="rijndael-256", $mode="cbc")
{
$this->algorithm = $algorithm;
$this->mode = $mode;
// open mcrypt module
$this->cipher = mcrypt_module_open ($this->algorithm, "", $this->mode, "");
}
public function __destruct()
{
if (is_resource ($this->cipher))
mcrypt_module_close ($this->cipher);
}
// NOTE: saving in a file just the output of the first element of the array
// returned isn't enough. You need to save the IV, otherwise
// the decrypt function won't work. I suggest you to do something like
// $iv . $enctext, and then a substr padding with the output
// of the function getIVSize() to get the IV and the enc. bindata.
// Also note that if you are using ECB mode, IV isn't needed.
// (IV = initialization vector)
public function encrypt ($text, $key1d, $key2d, $b64=true)
{
$cipher = $this->cipher;
// create IV and get keysize
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($cipher), MCRYPT_DEV_URANDOM);
$keysize = mcrypt_enc_get_key_size ($cipher);
// create the key
$key1 = base64_encode (hash ("sha256", $key1d, true));
$key2 = base64_encode (hash ("sha256", $key2d, true));
$key = substr ($key1, 0, $keysize / 2) . substr ($key2, ( round ( strlen ($key2) / 2 ) ), $keysize / 2);
$key = substr ($key . $key1 . $key2 . $key1, 0, $keysize);
// initialize encryption
mcrypt_generic_init ($cipher, $key, $iv);
// encrypt da shit
// for padding reasons we add the original length to the encrypted text
$enc = mcrypt_generic ($cipher, strlen ($text) . "|" . $text);
mcrypt_generic_deinit ($cipher);
return array (( $b64 ? base64_encode ($enc) : $enc ), $iv);
}
// You need to pass the returned IV from the encrypt function here,
// otherwise the decryption won't work. Read the note above the encrpytion
// function to learn more.
public function decrypt ($data, $iv, $key1d, $key2d)
{
// get keysize
$keysize = mcrypt_enc_get_key_size ($this->cipher);
// generate key
$key1 = base64_encode (hash ("sha256", $key1d, true));
$key2 = base64_encode (hash ("sha256", $key2d, true));
$key = substr ($key1, 0, $keysize / 2) . substr ($key2, ( round ( strlen ($key2) / 2 ) ), $keysize / 2);
$key = substr ($key . $key1 . $key2 . $key1, 0, $keysize);
// initialize decryption
mcrypt_generic_init ($this->cipher, $key, $iv);
$dec = mdecrypt_generic ($this->cipher, $data);
// get original length (@ is to suppress 'undefined index' errors)
@list ($len, $decshit) = explode ("|", $dec, 2);
if (!$len || !$decshit || !is_numeric ($len) || (is_numeric ($len) && $len > strlen ($dec)))
return false;
$dec = substr ($decshit, 0, $len);
mcrypt_generic_deinit ($this->cipher);
return $dec;
}
public function getIVSize ()
{
return mcrypt_enc_get_iv_size ($this->cipher);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment