diff --git a/src/PubNub/Crypto/AesCbcCryptor.php b/src/PubNub/Crypto/AesCbcCryptor.php index 0915ae71..2f80ffbc 100644 --- a/src/PubNub/Crypto/AesCbcCryptor.php +++ b/src/PubNub/Crypto/AesCbcCryptor.php @@ -45,7 +45,7 @@ public function encrypt(string $text, ?string $cipherKey = null): CryptoPayload return new CryptoPayload($encrypted, $iv, self::CRYPTOR_ID); } - public function decrypt(CryptoPayload $payload, ?string $cipherKey = null): string + public function decrypt(CryptoPayload $payload, ?string $cipherKey = null) { $text = $payload->getData(); $secret = $this->getSecret($cipherKey); diff --git a/src/PubNub/Crypto/Cryptor.php b/src/PubNub/Crypto/Cryptor.php index 0d6d9767..cf2bcac9 100644 --- a/src/PubNub/Crypto/Cryptor.php +++ b/src/PubNub/Crypto/Cryptor.php @@ -9,5 +9,5 @@ abstract class Cryptor public const CRYPTOR_ID = null; abstract public function encrypt(string $text, ?string $cipherKey = null): CryptoPayload; - abstract public function decrypt(CryptoPayload $payload, ?string $cipherKey = null): string; + abstract public function decrypt(CryptoPayload $payload, ?string $cipherKey = null); } diff --git a/src/PubNub/Crypto/LegacyCryptor.php b/src/PubNub/Crypto/LegacyCryptor.php index c07ffa4e..d09931d5 100644 --- a/src/PubNub/Crypto/LegacyCryptor.php +++ b/src/PubNub/Crypto/LegacyCryptor.php @@ -52,7 +52,7 @@ public function encrypt(string $text, ?string $cipherKey = null): Payload return new Payload($encryptedWithIV, '', self::CRYPTOR_ID); } - public function decrypt(Payload $payload, ?string $cipherKey = null): string + public function decrypt(Payload $payload, ?string $cipherKey = null) { $text = $payload->getData(); if (strlen($text) === 0) { diff --git a/src/PubNub/CryptoModule.php b/src/PubNub/CryptoModule.php index 98c94a4e..b2d52029 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()]) { @@ -157,4 +171,10 @@ public static function aesCbcCryptor(string $cipherKey, bool $useRandomIV): self aesCbcCryptor::CRYPTOR_ID ); } + + // for backward compatibility + public function getCipherKey() + { + return $this->cryptorMap[$this->defaultCryptorId]->getCipherKey(); + } }