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

环境

windows + php7.0以上即可

'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 '
';
    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)