Skip to content

Instantly share code, notes, and snippets.

@millsy
Created March 29, 2012 12:58
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save millsy/2237249 to your computer and use it in GitHub Desktop.
Save millsy/2237249 to your computer and use it in GitHub Desktop.
php asymmetric encryption/decryption using p12 file
<?php
$p12cert = array();
$file = 'https://myserver.com/myp12file.p12';
$c = file_get_contents($file);
if (openssl_pkcs12_read($c, $p12cert, '1234567890') )
{
$pkey = $p12cert['pkey']; //private key
$cert = $p12cert['cert']; //public key
//encrypt text
$text = "Hello World!";
if(openssl_public_encrypt($text, $crypted, $cert)){
echo base64_encode($crypted); //output crypted data base64 encoded
echo '<br/>';
if(openssl_private_decrypt($crypted, $decrypted, $pkey)){
echo $decrypted;
echo '<br/>';
}else{
echo 'failed to decrypt';
}
}else{
echo 'failed to encrypt';
}
}
else
{
echo 'Failed read p12 file';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment