php - openssl:如何从模数中获取公钥

标签 php openssl rsa modulus

我使用 openssl 生成一对 key :

shell> ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/mike/.ssh/id_rsa): /path/to/test_rsa
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /path/to/test_rsa.
Your public key has been saved in /path/to/test_rsa.pub.

然后,我从私钥生成模数:

shell> openssl rsa -in /path/to/test_rsa -noout -modulus > /path/to/modulus.txt

现在,有什么方法可以仅从模数中获取 test_rsa.pub(public key)?

最佳答案

您可以使用 phpseclib, a pure PHP RSA implementation 以更标准化的格式获取公钥。 .例如。

<?php
include('Crypt/RSA.php');

$modulus = 'yEQs2LxSHBZgZCH0rRQQy9kmry8g2tNhQL1B9f5azNz9Ce9pXPgSRjVUo1B9Ggb/FK3jy41wWd2IfS6rse3vBzRsabMj29CVODM/19yZPmwEmjJHCgYd+AA2qweKZanDp4FLsSw/kyV5WoPN16GHEMLmLGkJFNIWtzzH5jV+S80=';
$exponent = 'AQAB';

$rsa = new Crypt_RSA();

$modulus = new Math_BigInteger(base64_decode($modulus), 256);
$exponent = new Math_BigInteger(base64_decode($exponent), 256);

$rsa->loadKey(array('n' => $modulus, 'e' => $exponent));
$rsa->setPublicKey();

echo $rsa->getPublicKey();

关于php - openssl:如何从模数中获取公钥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16993838/

相关文章:

c - Openssl:RSA_verify()和X509_verify_cert()是否使用普通的RSA验证码

linux - 如何从 .cer 文件获取 CA 根证书和中间证书

math - 部分密文的RSA加密和解密?

java 设置指数长度

php - Sublime-phpcs - 无法使 PHP MD 和 CS 包正常运行

php - laravel 4 发送邮件时如何处理回调

c++ - 尝试使用OpenSSL EVP AES 256 C++时出现LINK错误

php - 在添加 www 和删除 .php 后添加尾部斜杠

php - 无法通过 CodeIgniter 连接到远程数据库

c++ - Windows TPM 虚拟智能卡 RSA key 存储