私KEY与公KEY 非对称加密

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>';
//得到结果



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值