美文网首页
php生成RSA公钥私钥方法-OPENSSL

php生成RSA公钥私钥方法-OPENSSL

作者: geeooooz | 来源:发表于2022-11-07 13:29 被阅读0次

    环境

    windows + php7.0以上即可

    <?php
    
    $config = array(
        'config'            =>'E:\wjwRanjian\wamp64\bin\php\php7.0.10\extras\ssl\openssl.cnf',//安装PHP7会自带这个配置文件
        'digest_alg'        => 'sha256', //可以用openssl_get_md_methods() 查看支持的加密方法
        'private_key_bits'=> 2048,
        'private_key_type'     => OPENSSL_KEYTYPE_RSA,
    );
        $res = openssl_pkey_new($config);
        openssl_pkey_export($res, $private_key_pem,null,$config);
    
        $details = openssl_pkey_get_details($res);
        $public_key_pem = $details['key'];
    
        echo '<pre>';
        var_dump(array('privKey' => $private_key_pem, 'pubKey' => $public_key_pem));
    
    E:\work\crm\test.php:16:
    array (size=2)
      'privKey' => string '-----BEGIN PRIVATE KEY-----
    MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQD2/cv5r+c+sZi8
    aWZgQ0PjI51kwUfgoytWi0Y/sFtLMkqZvhCxNfI7lAZAhwy34cQJV0kSTyRp3muC
    PsBJw13Fi/Do0VLKKBPhd7rNSN4RrzI7dsQTzUTXVkNCGnp0ny244hhNa6+zV4pE
    h1eUc8u6nl+BZ+xZpSsRGcHoDcyl0NqQTbNzx8/X4ZcaXlUvAy60XAVADqWTCldB
    uYBNE0R+t5P6XOgFGXzWHYL7lpA9QPu9e/P/rUb0LiIW/Q7bdjsXYC/OfctosuV9
    t0VVn+S2UPnfcSrp+pKRSppu60zOTN/86HPsHJvtyw5tQHUGKC1Tci9X4rXSwQaK
    q77D/r6vAgMBAAECggEBAO2nCKQIWDXfxnnL5MDNGD4un5w5yYJhHisYuZBAjadO
    Da/ezMrmgFxJMrbV+yvXZLwkK2Adn'... (length=1704)
      'pubKey' => string '-----BEGIN PUBLIC KEY-----
    MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA9v3L+a/nPrGYvGlmYEND
    4yOdZMFH4KMrVotGP7BbSzJKmb4QsTXyO5QGQIcMt+HECVdJEk8kad5rgj7AScNd
    xYvw6NFSyigT4Xe6zUjeEa8yO3bEE81E11ZDQhp6dJ8tuOIYTWuvs1eKRIdXlHPL
    up5fgWfsWaUrERnB6A3MpdDakE2zc8fP1+GXGl5VLwMutFwFQA6lkwpXQbmATRNE
    freT+lzoBRl81h2C+5aQPUD7vXvz/61G9C4iFv0O23Y7F2Avzn3LaLLlfbdFVZ/k
    tlD533Eq6fqSkUqabutMzkzf/Ohz7Byb7csObUB1BigtU3IvV+K10sEGiqu+w/6+
    rwIDAQAB
    -----END PUBLIC KEY-----
    ' (length=451)
    

    相关文章

      网友评论

          本文标题:php生成RSA公钥私钥方法-OPENSSL

          本文链接:https://www.haomeiwen.com/subject/idibtdtx.html