<?php
final class Encryption
{
// The key
const KEY = 'EB648D0BFCA30CADC75634B10C5A3A341F8BDC268069CA2F2EB79D3BF8D58D00';
/**
* Encrypt a string
*
* @access public
* @static
* @param string $string
* @return string
*/
public static function encrypt($string)
{
return mcrypt_encrypt(MCRYPT_RIJNDAEL_256, self::getMySQLKey(self::KEY), self::getPaddedString($string), MCRYPT_MODE_CBC, self::KEY);
}
/**
* Decrypt a string
*
* @access public
* @static
* @param string $string
* @return string
*/
public static function decrypt($string)
{
return rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, self::getMySQLKey(self::KEY), $string, MCRYPT_MODE_CBC, self::KEY), "\x00..\x10");
}
/**
* Get MySQL key
*
* @access public
* @static
* @param string $key
* @return string
*/
public static function getMySQLKey($key)
{
// The new key
$new_key = str_repeat(chr(0), 32);
// Iterate over the key and XOR
for ($i = 0, $l = strlen($key); $i < $l; ++$i)
{
$new_key[$i % 32] = $new_key[$i % 32] ^ $key[$i];
}
// Return the new key
return $new_key;
}
/**
* Get padded string
*
* @access public
* @static
* @param string $string
* @return string
*/
public static function getPaddedString($string)
{
return str_pad($string, (16 * (floor(strlen($string) / 16) + 1)), chr(16 - (strlen($string) % 16)));
}
}
echo base64_encode(Encryption::encrypt('michael@example.com'));
preferences:
32.97 ms | 402 KiB | 5 Q