Created
March 28, 2012 14:30
-
-
Save millsy/2226631 to your computer and use it in GitHub Desktop.
php asymmetric encryption/decryption
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
echo "Source: $source"; | |
$fp=fopen("/path/to/certificate.crt","r"); | |
$pub_key=fread($fp,8192); | |
fclose($fp); | |
openssl_get_publickey($pub_key); | |
/* | |
* NOTE: Here you use the $pub_key value (converted, I guess) | |
*/ | |
openssl_public_encrypt($source,$crypttext,$pub_key); | |
echo "String crypted: $crypttext"; | |
$fp=fopen("/path/to/private.key","r"); | |
$priv_key=fread($fp,8192); | |
fclose($fp); | |
// $passphrase is required if your key is encoded (suggested) | |
$res = openssl_get_privatekey($priv_key,$passphrase); | |
/* | |
* NOTE: Here you use the returned resource value | |
*/ | |
openssl_private_decrypt($crypttext,$newsource,$res); | |
echo "String decrypt : $newsource"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Taken from http://uk.php.net/manual/en/function.openssl-private-decrypt.php