/** * Sets the secret key. * * The key must be non-zero, and less than or equal to MAXKEYSIZE * characters (bytes) in length. * * @param string $key Key must be non-empty and less than MAXKEYSIZE * bytes in length. * @param string $iv The initialization vector to use. Only needed for * 'cbc' cipher. If null, an IV is automatically * generated. * * @throws Horde_Crypt_Blowfish_Exception */ public function setKey($key, $iv = null) { if (!is_string($key)) { throw new Horde_Crypt_Blowfish_Exception('Encryption key must be a string.'); } $len = strlen($key); if ($len > self::MAXKEYSIZE || $len == 0) { throw new Horde_Crypt_Blowfish_Exception(sprintf('Encryption key must be less than %d characters (bytes) and non-zero. Supplied key length: %d', self::MAXKEYSIZE, $len)); } $this->_crypt->key = $key; switch ($this->_crypt->cipher) { case 'cbc': if (is_null($iv)) { if (is_null($this->iv)) { $this->_crypt->setIv(); } } else { $iv = substr($iv, 0, self::IV_LENGTH); if (($len = strlen($iv)) < self::IV_LENGTH) { $iv .= str_repeat(chr(0), self::IV_LENGTH - $len); } $this->_crypt->setIv($iv); } break; case 'ecb': $this->iv = false; break; } }
/** */ public function __construct($cipher) { parent::__construct($cipher); $this->_mcrypt = mcrypt_module_open(MCRYPT_BLOWFISH, '', $cipher, ''); }