openssl RSA私钥和公钥

时间:2012-09-30 05:59:36

标签: openssl rsa

如果我使用open ssl命令

sudo openssl genrsa -out privkey.pem 2048

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

1 个答案:

答案 0 :(得分:15)

回答您的问题:使用sudo openssl genrsa -out privkey.pem 2048生成的文件包含私钥和公钥

openssl genrsa generates a key pair。公钥是模数(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:
    [...]

您只能使用-puboutopenssl rsa选项提取公钥:

$ 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)
$