Here are some examples on how to use openssl and php:
function encryptData($source, $privateKey)
{
$maxLength = 117;
$output = "";
while ($source)
{
$slice = substr($source, 0, $maxLength);
$source = substr($source, $maxLength);
openssl_private_encrypt($slice, $encrypted, $privateKey);
$output .= $encrypted;
}
return $output;
}
function decryptData($source, $publicKey)
{
$maxLength = 128;
$output = "";
while ($source)
{
$slice = substr($source, 0, $maxLength);
$source = substr($source, $maxLength);
openssl_public_decrypt($slice, $decrypted, $publicKey);
$output .= $decrypted;
}
return $output;
}
// usage
$myPrivateKey = ""; // your generated private key
$myPublicKey = ""; // your generated public key
$rawText = "lorem ipsum";
$crypted = encryptData($rawText, $myPrivateKey);
$decrypted = decryptData($crypted, $myPublicKey);
to generate your private/public key pair, just execute the following commands:
openssl genrsa -out private_key.pem 1024
openssl rsa -pubout -in private_key.pem -out public_key.pem
you will found two key on your current directory. If you need to add them onto variable, beware the whitespaces.