note 129940 added to function.openssl-pkey-new
| From: | Eno_CN@qq.com | Date: | Mon, 06 Jan 2025 03:21:44 +0000 |
| Subject: | note 129940 added to function.openssl-pkey-new | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-221753@lists.php.net to get a copy of this message | ||
Some examples for generating EC keypair
EC - generate keypair with curve_name
<?php
/*
* Custom parameters x, y, and d are not supported with SM2 in OpenSSL 3.x.
* Directly creating EVP_PKEY_CTX using EVP_PKEY_CTX_new_from_name(NULL, "SM2", NULL)
* will result in generating incorrect private keys (which cannot be correctly recognized
* by existing external applications based on the SM2 algorithm).
*/
$curve_name = 'SM2';
$pkey = openssl_pkey_new(array(
'ec'=> array(
'curve_name' => $curve_name,
)
));
$details = openssl_pkey_get_details($pkey);
var_dump($details);
$pubkey = $details['key'];
openssl_pkey_export($pkey, $prikey);
echo 'Private Key:', PHP_EOL, $prikey, PHP_EOL;
echo 'Public Keyï¼', PHP_EOL, $pubkey, PHP_EOL;
?>
EC - generate keypair with custom params (OSCCA WAPIP192v1 Elliptic curve)
<?php
$d = hex2bin('8D0AC65AAEA0D6B96254C65817D4A143A9E7A03876F1A37D'); // private key binary
$x = hex2bin('98E07AAD50C31F9189EBE6B8B5C70E5DEE59D7A8BC344CC6'); // public key x binary
$y = hex2bin('6109D3D96E52D0867B9D05D72D07BE5876A3D973E0E96792'); // public key y binary
$p = hex2bin('BDB6F4FE3E8B1D9E0DA8C0D46F4C318CEFE4AFE3B6B8551F');
$a = hex2bin('BB8E5E8FBC115E139FE6A814FE48AAA6F0ADA1AA5DF91985');
$b = hex2bin('1854BEBDC31B21B7AEFC80AB0ECD10D5B1B3308E6DBF11C1');
$g_x = hex2bin('4AD5F7048DE709AD51236DE65E4D4B482C836DC6E4106640');
$g_y = hex2bin('02BB3A02D4AAADACAE24817A4CA3A1B014B5270432DB27D2');
$order = hex2bin('BDB6F4FE3E8B1D9E0DA8C0D40FC962195DFAE76F56564677');
$pkey = openssl_pkey_new(array(
'ec'=> array(
'p' => $p,
'a' => $a,
'b' => $b,
'order' => $order,
'g_x' => $g_x,
'g_y' => $g_y,
//'d' => $d, // import the private key to generate keypairs
)
));
$details = openssl_pkey_get_details($pkey);
var_dump($details);
$pubkey = $details['key'];
openssl_pkey_export($pkey, $prikey);
echo 'Private Key:', PHP_EOL, $prikey, PHP_EOL;
echo 'Public Keyï¼', PHP_EOL, $pubkey, PHP_EOL;
?>
EC - generate keypair with custom params (SM2 curve)
<?php
$p = hex2bin('FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF');
$a = hex2bin('FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC');
$b = hex2bin('28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93');
$g_x = hex2bin('32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7');
$g_y = hex2bin('BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0');
$order = hex2bin('FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123');
/*
* Custom parameters x, y, and d are not supported with SM2 in OpenSSL 3.x.
* Directly creating EVP_PKEY_CTX using EVP_PKEY_CTX_new_from_name(NULL, "SM2", NULL)
* will result in generating incorrect private keys (which cannot be correctly recognized
* by existing external applications based on the SM2 algorithm).
*/
$pkey = openssl_pkey_new(array(
'ec'=> array(
'p' => $p,
'a' => $a,
'b' => $b,
'order' => $order,
'g_x' => $g_x,
'g_y' => $g_y,
)
));
/*
* It is not entirely the same as generating keys through the SM2 curve naming method.
* So the generated key will be in PKCS8 format to store algorithm information.
*/
$details = openssl_pkey_get_details($pkey);
var_dump($details);
$pubkey = $details['key'];
openssl_pkey_export($pkey, $prikey);
echo 'Private Key:', PHP_EOL, $prikey, PHP_EOL;
echo 'Public Keyï¼', PHP_EOL, $pubkey, PHP_EOL;
?>
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.137 (proxied: 218.104.205.82)
----
Manual Page -- https://php.net/manual/en/function.openssl-pkey-new.php
Edit -- https://main.php.net/note/edit/129940
Del: integrated -- https://main.php.net/note/delete/129940/integrated
Del: useless -- https://main.php.net/note/delete/129940/useless
Del: bad code -- https://main.php.net/note/delete/129940/bad+code
Del: spam -- https://main.php.net/note/delete/129940/spam
Del: non-english -- https://main.php.net/note/delete/129940/non-english
Del: in docs -- https://main.php.net/note/delete/129940/in+docs
Del: other reasons-- https://main.php.net/note/delete/129940
Reject -- https://main.php.net/note/reject/129940
Search -- https://main.php.net/manage/user-notes.php