/** * {@inheritDoc} * * @throws \UnexpectedValueException */ public function render(AttributeEvent $event) { $cipherText = $event->getValue(); // The Amazon SDK base64 encodes binary strings before it sends data to // DynamoDB, but it does not base64 decode it after it retrieves the // data from DynamoDB. Therefore we have to check wheter it is encoded. if (preg_match(self::BASE64_REGEX, $cipherText)) { $cipherText = base64_decode($cipherText); if (false === $cipherText) { throw new \UnexpectedValueException('Error decoding data in the ' . $event->getAttribute() . ' attribute'); } } $plainText = $this->cipher->decrypt($cipherText); if (false === $plainText) { throw new \UnexpectedValueException('Error decrypting data in the ' . $event->getAttribute() . ' attribute: Invalid key'); } $event->setValue($plainText); }
/** * Sets the key. * * Keys can be of any length. Blowfish, itself, requires the use of a key between 32 and max. 448-bits long. * If the key is less than 32-bits we NOT fill the key to 32bit but let the key as it is to be compatible * with mcrypt because mcrypt act this way with blowfish key's < 32 bits. * * If the key is more than 448-bits, we trim the excess bits. * * If the key is not explicitly set, or empty, it'll be assumed a 128 bits key to be all null bytes. * * @access public * @see Crypt_Base::setKey() * @param String $key */ function setKey($key) { $keylength = strlen($key); if (!$keylength) { $key = ""; } elseif ($keylength > 56) { $key = substr($key, 0, 56); } parent::setKey($key); }
/** * Setup the CRYPT_MODE_MCRYPT $engine * * @see Crypt_Base::_setupMcrypt() * @access private */ function _setupMcrypt() { $this->key = str_pad(substr($this->key, 0, $this->key_size), $this->key_size, ""); parent::_setupMcrypt(); }
/** * Sets the key. * * Keys can be of any length. RC2, itself, uses 1 to 1024 bit keys (eg. * strlen($key) <= 128), however, we only use the first 128 bytes if $key * has more then 128 bytes in it, and set $key to a single null byte if * it is empty. * * If the key is not explicitly set, it'll be assumed to be a single * null byte. * * @see Crypt_Base::setKey() * @access public * @param String $key * @param Integer $t1 optional Effective key length in bits. */ function setKey($key, $t1 = 0) { if ($t1 <= 0) { $t1 = $this->default_key_length; } else { if ($t1 > 1024) { $t1 = 1024; } } // Key byte count should be 1..128. $key = strlen($key) ? substr($key, 0, 128) : ""; $t = strlen($key); // The mcrypt RC2 implementation only supports effective key length // of 1024 bits. It is however possible to handle effective key // lengths in range 1..1024 by expanding the key and applying // inverse pitable mapping to the first byte before submitting it // to mcrypt. // Key expansion. $l = array_values(unpack('C*', $key)); $t8 = $t1 + 7 >> 3; $tm = 0xff >> 8 * $t8 - $t1; // Expand key. $pitable = $this->pitable; for ($i = $t; $i < 128; $i++) { $l[$i] = $pitable[$l[$i - 1] + $l[$i - $t]]; } $i = 128 - $t8; $l[$i] = $pitable[$l[$i] & $tm]; while ($i--) { $l[$i] = $pitable[$l[$i + 1] ^ $l[$i + $t8]]; } // Prepare the key for mcrypt. $l[0] = $this->invpitable[$l[0]]; array_unshift($l, 'C*'); parent::setKey(call_user_func_array('pack', $l)); }
/** * Test for engine validity * * This is mainly just a wrapper to set things up for Crypt_Base::isValidEngine() * * @see Crypt_Base::isValidEngine() * @param int $engine * @access public * @return bool */ function isValidEngine($engine) { if ($engine == CRYPT_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 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 Crypt_Base::encrypt() * @see Crypt_RC4::_crypt() * @access public * * @param String $ciphertext * * @return String $plaintext */ function decrypt($ciphertext) { if ($this->engine == CRYPT_MODE_MCRYPT) { return parent::decrypt($ciphertext); } return $this->_crypt($ciphertext, CRYPT_RC4_DECRYPT); }
/** * 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); }
/** * Decrypts a message. * * $this->decrypt($this->encrypt($plaintext)) == $this->encrypt($this->encrypt($plaintext)). * At least if the continuous buffer is disabled. * * @see Crypt_Base::encrypt() * @see self::_crypt() * @access public * @param string $ciphertext * @return string $plaintext */ function decrypt($ciphertext) { if ($this->engine != CRYPT_ENGINE_INTERNAL) { return parent::decrypt($ciphertext); } return $this->_crypt($ciphertext, CRYPT_RC4_DECRYPT); }
/** * 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 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); }
/** * Setup the CRYPT_ENGINE_MCRYPT $engine * * @see Crypt_Base::_setupMcrypt() * @access private */ function _setupMcrypt() { if (!isset($this->key)) { $this->setKey(''); } parent::_setupMcrypt(); }
/** * Test for engine validity * * This is mainly just a wrapper to set things up for Crypt_Base::isValidEngine() * * @see Crypt_Base::Crypt_Base() * @param int $engine * @access public * @return bool */ function isValidEngine($engine) { switch ($engine) { case CRYPT_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 CRYPT_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); }
/** * {@inheritDoc} */ public function transform(AttributeEvent $event) { $value = $event->getValue(); $event->setValue($this->cipher->encrypt($value)); }