【问题标题】:Generate Private and Public key OpenSSL生成私钥和公钥 OpenSSL
【发布时间】:2012-09-28 09:22:36
【问题描述】:

我有以下命令让 OpenSSL 生成私钥和公钥:

openssl genrsa –aes-128-cbc –out priv.pem –passout pass:[privateKeyPass] 2048

openssl req –x509 –new –key priv.pem –passin pass:[privateKeyPass] -days 3650 –out cert.cer

...但他们不工作。对于第一个命令,我收到以下错误:

usage: genrsa [args] [numbits]
 -des            encrypt the generated key with DES in cbc mode
 -des3           encrypt the generated key with DES in ede cbc mode (168 bit key)
 -seed
                 encrypt PEM output with cbc seed
 -aes128, -aes192, -aes256
                 encrypt PEM output with cbc aes
 -camellia128, -camellia192, -camellia256
                 encrypt PEM output with cbc camellia
 -out file       output the key to 'file
 -passout arg    output file pass phrase source
 -f4             use F4 (0x10001) for the E value
 -3              use 3 for the E value
 -engine e       use engine e, possibly a hardware device.
 -rand file:file:...
                 load the file (or the files in the directory) into
                 the random number generator

我做错了什么?

编辑: 我解决了第一个命令:

openssl genrsa -aes128 -out privkey.pem 2048

但现在我遇到了第二个错误:

unknown option –x509

【问题讨论】:

  • 不知道参数的顺序是否重要?
  • 命令中的一些破折号实际上是破折号 (-) 而不是连字符 (-)。

标签: openssl rsa private-key public-key


【解决方案1】:

'genrsa' 只生成一个 RSA 密钥。

'req' 然后使用该键发出 x509 样式请求。

如果您只需要 rsa 密钥对 - 使用 genrsa。

如果您需要密钥对和签名的 x509 请求,请使用“genrsa”,然后使用“req”。

“req”也可以选择为您生成该密钥(即它封装了“genrsa”命令(和 gendh)。

所以:

 openssl genrsa -aes128 -out privkey.pem 2048
 openssl req -new -x509 -key privkey.pem 

几乎等同于

 openssl req -new -x509 -keyout privkey.pem  -newkey rsa:2048

除了与“genrsa”不同的是,“req”不允许您将 aes128 指定为加密。

因此,在许多企业设置中,为了获得对所应用的密钥加密的充分控制,需要分两步进行。

【讨论】:

  • 这个命令成功了:openssl req -new -x509 -key new.pem -days 3650 -out cert.crt 非常感谢
  • 请注意,-x509 会生成自签名证书。如果要生成证书请求,请忽略此选项。
【解决方案2】:

从输出中可以看出,您选择了错误的算法。 你不应该通过-aes128而不是-aes-128-cbc吗?

从手册中我假设-aes-128-cbcopenssl enc 的正确参数,但我不知道它是否适用于genrsa

【讨论】: