public function testNonAESEncryptionSupport() { $blowfishParams = Crypto::getDefaultParams(['key' => Crypto::generateRandomKey(128), 'algorithm' => 'bf', 'mode' => 'ecb']); $encrypted = Crypto::encrypt('test', $blowfishParams); $decrypted = Crypto::decrypt($encrypted, $blowfishParams); $this->assertNotEquals($encrypted, $decrypted); $this->assertEquals('test', $decrypted); try { Crypto::getDefaultParams(['key' => Crypto::generateRandomKey(), 'algorithm' => 'fake']); } catch (\Exception $ex) { $this->assertInstanceOf('Ably\\Exceptions\\AblyException', $ex, 'Expected to raise an exception on unknown encryption mode'); } }
/** * Decodes message's data field according to encoding * @throws AblyException */ protected function decode() { $this->originalData = $this->data; $this->originalEncoding = $this->encoding; if (!empty($this->encoding)) { $encodings = explode('/', $this->encoding); foreach (array_reverse($encodings) as $encoding) { if ($encoding == 'base64') { $this->data = base64_decode($this->data); if ($this->data === false) { throw new AblyException('Could not base64-decode message data'); } array_pop($encodings); } else { if ($encoding == 'json') { $this->data = json_decode($this->data); if ($this->data === null) { throw new AblyException('Could not JSON-decode message data'); } array_pop($encodings); } else { if (strpos($encoding, 'cipher+') === 0) { if (!$this->cipherParams) { Log::e('Could not decrypt message data, no cipherParams provided'); break; } $data = Crypto::decrypt($this->data, $this->cipherParams); if ($data === false) { Log::e('Could not decrypt message data'); break; } $this->data = $data; array_pop($encodings); } } } } $this->encoding = count($encodings) ? implode('/', $encodings) : null; } }