/** * Setup the \Topxia\Service\Util\Phpsec\Crypt\Base::ENGINE_MCRYPT $engine * * @access private * @see \Topxia\Service\Util\Phpsec\Crypt\Base::_setupMcrypt() */ public function _setupMcrypt() { $this->key = str_pad(substr($this->key, 0, $this->key_size), $this->key_size, ""); parent::_setupMcrypt(); }
/** * Test for engine validity * * This is mainly just a wrapper to set things up for Crypt_Base::isValidEngine() * * @access public * @see \Topxia\Service\Util\Phpsec\Crypt\Base::isValidEngine() * * @param Integer $engine * @return Boolean */ public function isValidEngine($engine) { if ($engine == self::ENGINE_OPENSSL) { if (strlen($this->key) != 16) { return false; } $this->cipher_name_openssl_ecb = 'bf-ecb'; $this->cipher_name_openssl = 'bf-' . $this->_openssl_translate_mode(); } return parent::isValidEngine($engine); }
/** * Sets the key. * * Keys can be of any length. Twofish, itself, requires the use of a key that's 128, 192 or 256-bits long. * If the key is less than 256-bits we round the length up to the closest valid key length, * padding $key with null bytes. If the key is more than 256-bits, we trim the excess bits. * * If the key is not explicitly set, it'll be assumed a 128 bits key to be all null bytes. * * @access public * @see \Topxia\Service\Util\Phpsec\Crypt\Base::setKey() * * @param String $key */ public function setKey($key) { $keylength = strlen($key); switch (true) { case $keylength <= 16: $key = str_pad($key, 16, ""); break; case $keylength <= 24: $key = str_pad($key, 24, ""); break; case $keylength < 32: $key = str_pad($key, 32, ""); break; case $keylength > 32: $key = substr($key, 0, 32); } parent::setKey($key); }
/** * Decrypts a message. * * $this->decrypt($this->encrypt($plaintext)) == $this->encrypt($this->encrypt($plaintext)). * At least if the continuous buffer is disabled. * * @access public * @see \Topxia\Service\Util\Phpsec\Crypt\Base::encrypt() * @see \Topxia\Service\Util\Phpsec\Crypt\RC4::_crypt() * * @param String $ciphertext * @return String $plaintext */ public function decrypt($ciphertext) { if ($this->engine != Base::ENGINE_INTERNAL) { return parent::decrypt($ciphertext); } return $this->_crypt($ciphertext, self::DECRYPT); }
/** * Setup the \Topxia\Service\Util\Phpsec\Crypt\Base::ENGINE_MCRYPT $engine * * @access private * @see \Topxia\Service\Util\Phpsec\Crypt\Base::_setupMcrypt() */ public function _setupMcrypt() { if (!isset($this->key)) { $this->setKey(''); } parent::_setupMcrypt(); }