warn ( 'Using the default key for crypto' ); } if ( ! self::$encryptionOff ) { self::$algortithm = ( Cfg::get ( 'quercus', false ) ) ? MCRYPT_TRIPLEDES : MCRYPT_RIJNDAEL_256; } self::$instance = new Cryptography ( self::$randKey ); } /** * General encryption method * @param string $plainText * @return string */ public static function en ( $plainText ) { return self::$instance->encrypt ( $plainText ); } /** * generalized decryption methd. * @param string $cypherText * @return string */ public static function de ( $cypherText ) { return self::$instance->decrypt ( $cypherText ); } public static function factory ( $l_key=null ) { return new Cryptography( $l_key ); } private $td; private $blockLength; /** * * @param string $l_key Key to use for encryption */ public function __construct ( $l_key=null ) { parent::__construct(); if ( self::$encryptionOff ) { return; } $plainTextKey = ( $l_key == null ) ? self::$randKey : $l_key; $keySize = mcrypt_get_key_size ( self::$algortithm, MCRYPT_MODE_ECB ); while ( strlen ( $plainTextKey ) < $keySize ) $plainTextKey .= $plainTextKey; $plainTextKey = substr ( $plainTextKey, 0, $keySize ); $this->blockLength = mcrypt_get_block_size ( self::$algortithm, MCRYPT_MODE_ECB ); $this->td = mcrypt_module_open ( self::$algortithm, '', MCRYPT_MODE_ECB, '' ); $iv = mcrypt_create_iv ( mcrypt_enc_get_iv_size ( $this->td ), MCRYPT_RAND ); mcrypt_generic_init ( $this->td, $plainTextKey, $iv ); } /** * encrypt the passed string * @param string $input * @return string */ public function encrypt ( $plainText, $force=false ) { if ( self::$encryptionOff && ! $force ) return $plainText; if ( ! isset ( $plainText ) || strlen ( $plainText ) == 0 ) { return $plainText; } $len = strlen ( $plainText ) % $this->blockLength; if ( $len != 0 ) { $charactersNeeded = $this->blockLength - $len; $plainText .= substr ( self::PADDING, 0, $charactersNeeded ); } $m = mcrypt_generic ( $this->td, $plainText ); $cypherText = self::META . base64_encode ( $m ); return $cypherText; } public function decrypt ( $cypherText ) { if ( strpos ( $cypherText, self::META ) !== 0 ) return $cypherText; $plainText = trim ( mdecrypt_generic ( $this->td, base64_decode ( substr ( $cypherText, self::META_LEN ) ) ) ); return $plainText; } /** * Clean up the crypto resources when they go out of scope */ public function __destruct() { if ( function_exists ( 'mcrypt_generic_deinit' ) && $this->td != null ) mcrypt_generic_deinit ( $this->td ); if ( function_exists ( 'mcrypt_module_close' ) && $this->td != null ) mcrypt_module_close ( $this->td ); } }