How can one encode and decode data using the generated public and secret key pair in PHP with mcrypt?

To encode and decode data using a generated public and secret key pair in PHP with mcrypt, you can use the mcrypt_encrypt and mcrypt_decrypt functions. First, generate a key pair using mcrypt_create_iv, then use the public key to encrypt the data and the secret key to decrypt it.

// Generate key pair
$publicKey = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_DEV_URANDOM);
$secretKey = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_DEV_URANDOM);

// Encode data
$data = "Hello, world!";
$encryptedData = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $publicKey, $data, MCRYPT_MODE_CBC);

// Decode data
$decryptedData = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $secretKey, $encryptedData, MCRYPT_MODE_CBC);

echo "Original data: " . $data . "\n";
echo "Encrypted data: " . base64_encode($encryptedData) . "\n";
echo "Decrypted data: " . $decryptedData . "\n";