6

如果我使用 open ssl 命令

sudo openssl genrsa -out privkey.pem 2048

要生成 rsa 密钥,它只生成 1 个文件。这是私钥。我如何获得公钥。

4

1 回答 1

15

回答您的问题:您生成的文件同时sudo openssl genrsa -out privkey.pem 2048包含private 和 public key

openssl genrsa 生成一个密钥对。公钥是模数 (n)公共指数 (e)

$ openssl genrsa 2048 > dummy.key
$ openssl rsa -in dummy.key -noout -text

Private-Key: (2048 bit)
modulus:
    00:d4:4a:3c:8c:41:b3:9b:a3:26:2d:4b:8c:62:08:
    c0:fc:ad:6c:5e:5f:3f:28:e6:a3:7c:3d:43:5e:98:
    [...]
publicExponent: 65537 (0x10001)
privateExponent:
    00:c7:6e:f6:72:cd:46:6e:70:56:ed:36:8c:2b:8d:
    0d:c0:53:2b:fb:7c:7f:59:6d:53:28:e4:64:e5:f9:
    d6:84:64:7e:e4:be:20:64:7d:5b:50:06:ba:1f:df:
    [...]

您可以使用openssl rsa的-pubout选项仅提取公钥:

$ openssl rsa -in dummy.key -pubout
writing RSA key
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1Eo8jEGzm6MmLUuMYgjA
[...]
gW6qhadXVsKWQhEhVU9s7V9vJOhfeSSahvuZR0zjursTm6PSJWo7lKNgFCFC0NXO
LQIDAQAB
-----END PUBLIC KEY-----

如果您将该数据提取到一个文件中并重新读取它,您将看到它包含模数和指数:

$ openssl rsa -in dummy.key -pubout > public.key
$ openssl rsa -in public.key -pubin -noout -text
Public-Key: (2048 bit)
Modulus:
    00:d4:4a:3c:8c:41:b3:9b:a3:26:2d:4b:8c:62:08:
    c0:fc:ad:6c:5e:5f:3f:28:e6:a3:7c:3d:43:5e:98:
    [...]
    6f:24:e8:5f:79:24:9a:86:fb:99:47:4c:e3:ba:bb:
    13:9b:a3:d2:25:6a:3b:94:a3:60:14:21:42:d0:d5:
    ce:2d
Exponent: 65537 (0x10001)
$
于 2012-09-30T06:14:22.080 回答