这里演示了公钥加密、私钥解密 和私钥加密、公钥解密的实例:
<?php
$publickey = file_get_contents("/etc/ssl/crt/public.der");
$privatekey= file_get_contents("/etc/ssl/crt/private.key");
$plaintext = "String to encrypt";
echo "test : encrypt 10000 times\n";
$st = time();
for($i = 0; $i < 10000; $i++){
openssl_public_encrypt($plaintext, $encrypted, $publickey);
}
$et = time();
echo "use time:".($et–$st)."\n";
echo "public encrypt and private decrypt:\n";
echo $encrypted; //encrypted string
openssl_private_decrypt($encrypted, $plain, $privatekey);
echo "\n\n";
echo $plain."\n";
echo "private encrypt and public decrypt:\n";
openssl_private_encrypt($plaintext, $encrypted, $privatekey);
echo $encrypted; //encrypted string
echo "\n";
openssl_public_decrypt($encrypted, $plain, $publickey);
echo $plain;
echo "\n";
?>