- var_dump: documentation ( source)
- base64_decode: documentation ( source)
- base64_encode: documentation ( source)
<?php
$pubkey = '...public key here...';
$privkey = '...private key here...';
function encrypt($data)
{
global $pubkey, $privkey;
if (openssl_public_encrypt($data, $encrypted, $pubkey))
$data = base64_encode($encrypted);
else
throw new Exception('Unable to encrypt data. Perhaps it is bigger than the key size?');
return $data;
}
function decrypt($data)
{
global $pubkey, $privkey;
if (openssl_private_decrypt(base64_decode($data), $decrypted, $privkey))
$data = $decrypted;
else
$data = '';
return $data;
}
$e = encrypt('hello');
var_dump($e);
var_dump(decrypt($e));