//创建私钥个共钥
<?php //echo phpinfo();exit; //生成私钥对象 //$privatekeyobj = openssl_pkey_new(); //利用私钥对象并保存成文件 //openssl_pkey_export_to_file($privatekeyobj,"./prikey.txt"); //获取私钥对象 $privatekeyobj = openssl_get_privatekey(file_get_contents("./prikey.txt")); //根据私钥对象生成共钥 $publickey = openssl_pkey_get_details($privatekeyobj)['key']; file_put_contents("./pubkey.txt",$publickey); //var_dump($publickey);
//运用
<?php /** * Created by PhpStorm. * Date: 2017/11/30 * Time: 19:54 * @content 利用共钥和私钥实现数据的加密传输 */ $pubkey = openssl_get_publickey(file_get_contents("./pubkey.txt")); $str = 'aaa1bbbb2cccc4'; //共钥加密 openssl_public_encrypt($str,$res,$pubkey); $prikey = openssl_get_privatekey(file_get_contents("./prikey.txt")); openssl_private_decrypt($res,$re,$prikey); var_dump($re);