test.php
<?php
//第一步,新建一个KEY$config = array(
'config'=>'d:\test\openssl.cnf',
"digest_alg" => "sha512",
"private_key_bits" => 1024,
"private_key_type" => 0,
);
$rs=openssl_pkey_new($config);
//第二步,私钥,和公钥保存
openssl_pkey_export($rs, $out,null,$config);
file_put_contents('d:\test\pri.txt', $out);
//保存完私钥后,取出公钥。
$pri_key_detail=openssl_pkey_get_details($rs);
file_put_contents('d:\test\pub.txt', $pri_key_detail['key']);
echo 'OK';
test1.php
<?php
//取出私钥和公钥
$origin_txt='我是原文25qwfr*^*(#$&(fH';
$pri_key_txt=file_get_contents('d:\test\pri.txt');
$pub_key_txt=file_get_contents('d:\test\pub.txt');
$pri_key=openssl_get_privatekey($pri_key_txt);
$pub_key=openssl_get_publickey($pub_key_txt);
var_dump(openssl_pkey_get_details($pri_key));
var_dump(openssl_pkey_get_details($pub_key));
exit;
//私钥加密,公解。
openssl_private_encrypt($origin_txt, $crypted, $pri_key);
echo 'openssl_private_encrypt $pri_key : '.$crypted.'<hr>';
var_dump($pub_key);
openssl_public_decrypt($crypted, $decrypted, $pub_key);
echo 'openssl_private_decrypt $pri_key : '.$decrypted.'<hr>';
//私钥加密,公解。
openssl_public_encrypt($origin_txt, $crypted, $pub_key);
echo 'openssl_private_encrypt $pri_key : '.$crypted.'<hr>';
openssl_private_encrypt($crypted, $decrypted, $pri_key);
echo 'openssl_private_decrypt $pri_key : '.$decrypted.'<hr>';
//得到结果