/** * Test for engine validity * * This is mainly just a wrapper to set things up for \phpseclib\Crypt\Base::isValidEngine() * * @see \phpseclib\Crypt\Base::isValidEngine() * @param int $engine * @access public * @return bool */ function isValidEngine($engine) { if ($engine == self::ENGINE_OPENSSL) { if ($this->key_length != 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 \phpseclib\Crypt\Base::setKey() * @param String $key */ 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. * * @see \phpseclib\Crypt\Base::encrypt() * @see \phpseclib\Crypt\RC4::_crypt() * @access public * @param String $ciphertext * @return String $plaintext */ function decrypt($ciphertext) { if ($this->engine != Base::ENGINE_INTERNAL) { return parent::decrypt($ciphertext); } return $this->_crypt($ciphertext, self::DECRYPT); }
/** * Setup the \phpseclib\Crypt\Base::ENGINE_MCRYPT $engine * * @see \phpseclib\Crypt\Base::_setupMcrypt() * @access private */ function _setupMcrypt() { if (!isset($this->key)) { $this->setKey(''); } parent::_setupMcrypt(); }
/** * This function deinitializes an encryption module * * This function terminates encryption specified by the encryption descriptor (td). * It clears all buffers, but does not close the module. You need to call * mcrypt_module_close() yourself. (But PHP does this for you at the end of the * script.) * * @param \phpseclib\Crypt\Base $td * @return bool * @access public */ function phpseclib_mcrypt_generic_deinit(Base $td) { if (!isset($td->mcrypt_polyfill_init)) { trigger_error('mcrypt_generic_deinit(): Could not terminate encryption specifier', E_USER_WARNING); return false; } $td->disableContinuousBuffer(); unset($td->mcrypt_polyfill_init); return true; }
/** * Setup the \phpseclib\Crypt\Base::ENGINE_MCRYPT $engine * * @see \phpseclib\Crypt\Base::_setupMcrypt() * @access private */ 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 \phpseclib\Crypt\Base::isValidEngine() * * @see \phpseclib\Crypt\Base::__construct() * @param int $engine * @access public * @return bool */ function isValidEngine($engine) { switch ($engine) { case self::ENGINE_OPENSSL: if ($this->block_size != 16) { return false; } $this->cipher_name_openssl_ecb = 'aes-' . ($this->key_length << 3) . '-ecb'; $this->cipher_name_openssl = 'aes-' . ($this->key_length << 3) . '-' . $this->_openssl_translate_mode(); break; case self::ENGINE_MCRYPT: $this->cipher_name_mcrypt = 'rijndael-' . ($this->block_size << 3); if ($this->key_length % 8) { // is it a 160/224-bit key? // mcrypt is not usable for them, only for 128/192/256-bit keys return false; } } return parent::isValidEngine($engine); }
/** * Sets the key. * * Keys can be of any length. DES, itself, uses 64-bit keys (eg. strlen($key) == 8), however, we * only use the first eight, if $key has more then eight characters in it, and pad $key with the * null byte if it is less then eight characters long. * * DES also requires that every eighth bit be a parity bit, however, we'll ignore that. * * If the key is not explicitly set, it'll be assumed to be all zero's. * * @see \phpseclib\Crypt\Base::setKey() * @access public * @param string $key */ function setKey($key) { // We check/cut here only up to max length of the key. // Key padding to the proper length will be done in _setupKey() if (strlen($key) > $this->key_size_max) { $key = substr($key, 0, $this->key_size_max); } // Sets the key parent::setKey($key); }
/** * Sets the key length. * * Valid key lengths are 128, 192 or 256 bits * * @access public * @param int $length */ function setKeyLength($length) { switch (true) { case $length <= 128: $this->key_length = 16; break; case $length <= 192: $this->key_length = 24; break; default: $this->key_length = 32; } parent::setKeyLength($length); }