diff --git a/src/PubNub/CryptoModule.php b/src/PubNub/CryptoModule.php index 98c94a4e..7ace2d2a 100644 --- a/src/PubNub/CryptoModule.php +++ b/src/PubNub/CryptoModule.php @@ -48,7 +48,7 @@ public function registerCryptor(Cryptor $cryptor, ?string $cryptorId = null): se return $this; } - protected function stringify(mixed $data): string + protected function stringify($data): string { if (is_string($data)) { return $data; @@ -57,7 +57,7 @@ protected function stringify(mixed $data): string } } - public function encrypt(mixed $data, ?string $cryptorId = null): string + public function encrypt($data, ?string $cryptorId = null): string { if (($data) == '') { throw new PubNubResponseParsingException("Encryption error: message is empty"); @@ -70,12 +70,26 @@ public function encrypt(mixed $data, ?string $cryptorId = null): string return base64_encode($header . $cryptoPayload->getData()); } - public function decrypt(string $input) + public function decrypt($cipherText) { - if (strlen($input) == '') { + if (is_array($cipherText)) { + if (array_key_exists("pn_other", $cipherText)) { + $cipherText = $cipherText["pn_other"]; + } else { + if (is_array($cipherText)) { + throw new PubNubResponseParsingException("Decryption error: message is not a string"); + } else { + throw new PubNubResponseParsingException("Decryption error: pn_other object key missing"); + } + } + } elseif (!is_string($cipherText)) { + throw new PubNubResponseParsingException("Decryption error: message is not a string or object"); + } + + if (strlen($cipherText) == '') { throw new PubNubResponseParsingException("Decryption error: message is empty"); } - $data = base64_decode($input); + $data = base64_decode($cipherText); $header = $this->decodeHeader($data); if (!$this->cryptorMap[$header->getCryptorId()]) { diff --git a/src/PubNub/PNConfiguration.php b/src/PubNub/PNConfiguration.php index a24b0544..7b6062e5 100755 --- a/src/PubNub/PNConfiguration.php +++ b/src/PubNub/PNConfiguration.php @@ -58,6 +58,8 @@ class PNConfiguration private $usingUserId = null; + private string $cipherKey; + /** * PNConfiguration constructor. */ @@ -122,7 +124,7 @@ public function setSecretKey($secretKey) */ public function getCipherKey() { - return $this->getCrypto()->getCipherKey(); + return $this->cipherKey; } public function isAesEnabled() @@ -136,6 +138,7 @@ public function isAesEnabled() */ public function setCipherKey($cipherKey) { + $this->cipherKey = $cipherKey; if ($this->crypto == null) { $this->crypto = CryptoModule::legacyCryptor($cipherKey, $this->getUseRandomIV()); } else {