跳至正文

OpenSSL 生成不对称密钥(公钥、私钥)

  • 后端
protected function createOpensslKey()
{
	// 生成私钥
	$privateKey = openssl_pkey_new(array(
		'private_key_bits' => 2048,
		'private_key_type' => OPENSSL_KEYTYPE_RSA,
	));
	openssl_pkey_export($privateKey, $privateKeyString);
	// 生成公钥
	$publicKey = openssl_pkey_get_details($privateKey);
	$publicKeyString = $publicKey['key'];
	$opensslKey = [
		'privateKey' => $privateKeyString,
		'publicKey' => $publicKeyString
	];
	return $opensslKey;
}