How can RSA encryption be implemented using PHP's built-in functions?

To implement RSA encryption using PHP's built-in functions, you can use the openssl_public_encrypt() function to encrypt data with a public key and openssl_private_decrypt() function to decrypt it with a private key. First, generate a key pair using openssl_pkey_new() function, then extract the public and private keys using openssl_pkey_get_details(). You can then use these keys to encrypt and decrypt data.

// Generate key pair
$resource = openssl_pkey_new();
openssl_pkey_export($resource, $privateKey);
$keyDetails = openssl_pkey_get_details($resource);
$publicKey = $keyDetails['key'];

// Encrypt data with public key
$data = "Hello, world!";
openssl_public_encrypt($data, $encrypted, $publicKey);

// Decrypt data with private key
openssl_private_decrypt($encrypted, $decrypted, $privateKey);

echo "Original data: $data\n";
echo "Encrypted data: $encrypted\n";
echo "Decrypted data: $decrypted\n";