/** * * @see \CryptoUtil\PBE\PBEScheme::decryptWithKey() * @throws \UnexpectedValueException If decryption failed * @return string */ public function decryptWithKey($data, $key) { if (strlen($key) !== 16) { throw new \UnexpectedValueException("Invalid key length."); } try { $algo = $this->_cipher->withInitializationVector(substr($key, 8, 8)); $str = $this->_crypto->decrypt($data, substr($key, 0, 8), $algo); return $this->_removePadding($str, 8); } catch (\RuntimeException $e) { throw new \UnexpectedValueException("Decryption failed.", null, $e); } }
/** * Get OpenSSL cipher method for given cipher algorithm identifier. * * @param CipherAlgorithmIdentifier $algo * @throws \UnexpectedValueException * @return string */ protected function _algoToCipher(CipherAlgorithmIdentifier $algo) { $oid = $algo->oid(); if (array_key_exists($oid, self::MAP_CIPHER_OID_TO_NAME)) { return self::MAP_CIPHER_OID_TO_NAME[$oid]; } if (AlgorithmIdentifier::OID_RC2_CBC == $oid) { if (!$algo instanceof RC2CBCAlgorithmIdentifier) { throw new \UnexpectedValueException("Not an RC2-CBC algorithm."); } return $this->_rc2AlgoToCipher($algo); } throw new \UnexpectedValueException("Cipher method " . $algo->oid() . " not supported."); }