Skip to content

Instantly share code, notes, and snippets.

@millsy
Created March 28, 2012 14:30
Show Gist options
  • Save millsy/2226631 to your computer and use it in GitHub Desktop.
Save millsy/2226631 to your computer and use it in GitHub Desktop.
php asymmetric encryption/decryption
<?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";
?>
@millsy
Copy link
Author

millsy commented Mar 28, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment