Created
March 29, 2012 12:58
-
-
Save millsy/2237249 to your computer and use it in GitHub Desktop.
php asymmetric encryption/decryption using p12 file
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 | |
$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