利用RSA公钥和私钥可以对字符串进行加解密,当没有公钥和私钥时可以生成一对密钥对进行测试

// 配置参数
$config = [
    "private_key_bits" => 2048, // 私钥位数,通常建议使用 2048 位或更高
    "private_key_type" => OPENSSL_KEYTYPE_RSA, // 密钥类型为 RSA
];
// 生成新的私钥和公钥
$res = openssl_pkey_new($config);
// 错误检测
if (!$res) {
    $error = openssl_error_string();
    echo "密钥对生成失败: " . $error;
}
// 提取私钥
openssl_pkey_export($res, $privateKey);
// 获取公钥详情
$publicKey = openssl_pkey_get_details($res);
$publicKey = $publicKey["key"];
// 输出公钥和私钥
echo "公钥:<br>";
echo $publicKey . "<br>";
echo "私钥:<br>";
echo $privateKey . "<br>";
// 释放资源
openssl_pkey_free($res);