diff --git a/src/Traits/Decrypt.php b/src/Traits/Decrypt.php index 416b6b0..9a5bcdb 100644 --- a/src/Traits/Decrypt.php +++ b/src/Traits/Decrypt.php @@ -12,8 +12,8 @@ public function parseCipherData($cipherData) return [ 'cipherText' => base64_decode($cipherParts[0]), - 'version' => base64_decode($cipherParts[1]), - 'iv' => base64_decode($cipherParts[2]), + 'version' => base64_decode($cipherParts[1]), + 'iv' => base64_decode($cipherParts[2]), ]; } @@ -41,15 +41,17 @@ protected function performDecryption($cipherData, $key): string [ 'cipherText' => $cipherText, - 'version' => $version, - 'iv' => $algorithmIv + 'version' => $version, + 'iv' => $algorithmIv ] = $this->parseCipherData($cipherData); $encryptionVersion = $this->getVersion($version); $algorithm = $this->versions[$encryptionVersion]; if (openssl_open($cipherText, $decryptedData, base64_decode($key), $privateKey, $algorithm, $algorithmIv)) { - openssl_free_key($privateKey); + if (\PHP_VERSION_ID < 80000) { + openssl_free_key($privateKey); + } return $decryptedData; } diff --git a/src/Traits/Encrypt.php b/src/Traits/Encrypt.php index d7768f0..6d2c06d 100644 --- a/src/Traits/Encrypt.php +++ b/src/Traits/Encrypt.php @@ -24,12 +24,12 @@ protected function performEncryption($data, $version = null): array $algorithmIv = $this->generateIV($encryptionVersion); $algorithm = $this->versions[$encryptionVersion]; - $publicKeys = collect($this->publicKey)->map(function($publicKey, $id) { - $key = openssl_pkey_get_public($publicKey); - if(!$key){ + $publicKeys = collect($this->publicKey)->map(function ($publicKey, $id) { + $key = openssl_pkey_get_public($publicKey); + if (! $key) { Log::critical('Public key id: [' . $id . '] Is invalid'); - } - return $key; + } + return $key; })->filter(); $encryptedData = null; @@ -41,12 +41,14 @@ protected function performEncryption($data, $version = null): array // Ensure each shareKey is labelled with its corresponding key id foreach ($publicKeys as $keyId => $publicKey) { $mappedKeys[$keyId] = base64_encode($envelopeKeys[$i]); - openssl_free_key($publicKey); + if (\PHP_VERSION_ID < 80000) { + openssl_free_key($publicKey); + } $i++; } return [ - 'keys' => $mappedKeys, + 'keys' => $mappedKeys, 'cipherText' => base64_encode($encryptedData) . ':' . base64_encode($encryptionVersion)