encryptSessionAesKey($sessionAesKey); return "v1$" . $sessionId . "$" . $encryptedSessionAesKey; } protected function encryptSessionAesKey(string $sessionAesKey): string { $rsa = PublicKeyLoader::load($this->base64PublicKey)->withPadding(RSA::ENCRYPTION_OAEP)->withHash('sha1')->withMGFHash('sha1'); return base64_encode($rsa->encrypt($sessionAesKey)); } protected function encryptData(string $sessionId, string $secretKey, string $iv, string $data): string { $cipher = new AES('gcm'); $cipher->setKey(base64_decode($secretKey)); $cipher->setNonce(base64_decode($iv)); $cipher->setAAD(base64_decode($secretKey)); $encrypted = $cipher->encrypt($data); $combined = base64_encode($encrypted . $cipher->getTag()); return 'v1$' . $sessionId . '$' . $combined; } protected function decryptData(string $secretKey, string $iv, string $encryptedData): string { $parsed = base64_decode(explode('$', $encryptedData)[2]); $encrypted = substr($parsed, 0, strlen($parsed) - 16); $tag = substr($parsed, strlen($parsed) - 16); $cipher = new AES('gcm'); $cipher->setKey(base64_decode($secretKey)); $cipher->setNonce(base64_decode($iv)); $cipher->setAAD(base64_decode($secretKey)); $cipher->setTag($tag); return $cipher->decrypt($encrypted); } }