/** * Encrypt * * @param string $plaintext * @param string $publicKey * @return string * @throws RuntimeException */ public function encrypt(string $plaintext, string $publicKey = '') : string { // generate a random session key $sessionKey = random_bytes($this->symmetric->getKeySize()); // encrypt the plaintext with symmetric algorithm $ciphertext = $this->symmetric->encrypt($plaintext, $sessionKey); // encrypt the session key with publicKey $encryptedKey = $this->public->encrypt($sessionKey, $publicKey); // openssl_public_encrypt($sessionKey, $encryptedKey, $publicKey, $padding); return base64_encode($encryptedKey) . ':' . $ciphertext; }