php openssl

 

$config = [
    'digest_alg' => 'sha256',
    'private_key_bits' => '512',
    'pricate_key_type' => OPENSSL_KEYTYPE_RSA,
];
$res = openssl_pkey_new($config);
openssl_pkey_export($res, $rsaPrivKey);
$pubKey = openssl_pkey_get_details($res);
$rsaPubKey = $pubKey['key'];

 这样便可获取生成的公钥$rsaPubKey和私钥$rsaPrivKey

问题一:

$res返回false的时候,检查发现,是window系统缺少了openssl环境变量,解决方法如下:

第一种是给传入的$config添加'config'值,对应php下openssl.cnf的文件地址

$config['config'] = 'D:\bin\php\extras\ssl\openssl.cnf';

 openssl_pkey_export 的 $configargs 也要传下,不然会返回null

这种不太方便,还有另一种方法,改变本地openssl.cnf的指向

首先在phpinfo中找到如下图所示

创建对应的路径,将php中的openssl.cnf复制进去,然后就可以尝试测试结果了

 

https://www.cnblogs.com/andydao/p/10365031.html

https://zhidao.baidu.com/question/1797682641931562907.html

https://www.cnblogs.com/wadhf/p/12041267.html

 

posted @ 2020-08-28 10:44  程序生(Codey)  阅读(309)  评论(0编辑  收藏  举报