【问题标题】:openssl: how can i get public key from modulusopenssl:如何从模数中获取公钥
【发布时间】:2013-06-04 08:03:02
【问题描述】:

我使用 openssl 生成一对密钥:

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) 吗?

【问题讨论】:

  • 也许我错了,它必须使用指数加法。但是,如果我有模数和指数,我可以得到公钥吗?
  • 我找到了一个帖子stackoverflow.com/questions/15420259/…,但是它使用了object-c,实际上我想要一个PHP解决方案。
  • 一个 RSA 密钥是一对整数 exponent,modulus。因此,答案是否定的,您不能仅从模数创建可用密钥。尽管许多实现确实使用固定的公钥指数,例如65537
  • @Mike.G 我认为你应该更新你的问题以表明你想要来自模数和指数的公钥。

标签: php openssl rsa modulus


【解决方案1】:

您可以使用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();

【讨论】:

  • 有没有办法在没有 phpseclib 库的情况下实现这一目标?
猜你喜欢
  • 2012-09-10
  • 1970-01-01
  • 2012-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-18
  • 2012-01-02
相关资源
最近更新 更多