前言 一篇OpenSSL 密钥对生成工具个人使用经验,不定期更新。
获取帮助
输出如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 Usage: genpkey [options] General options: -help Display this summary -engine val Use engine, possibly a hardware device -paramfile infile Parameters file -algorithm val The public key algorithm -quiet Do not output status while generating keys -pkeyopt val Set the public key algorithm option as opt:value -config infile Load a configuration file (this may load modules) Output options: -out outfile Output file -outform PEM|DER output format (DER or PEM) -pass val Output file pass phrase source -genparam Generate parameters, not key -text Print the in text -* Cipher to use to encrypt the key Provider options: -provider-path val Provider load path (must be before 'provider' argument if required) -provider val Provider to load (can be specified multiple times ) -propquery val Property query used when fetching algorithms Order of options may be important! See the documentation.
简单的生成密钥对指令
私钥生成 为了生成一个密钥对,至少需要指定一个算法,指定输出文件的名称。
例如以下命令,它将会生成一个Ed25519私钥文件,文件名是private-key-Ed25519.pem
:
1 openssl genpkey -algorithm Ed25519 -out private-key-Ed25519.pem
公钥提取 可以从私钥文件中提取公钥。
例如以下命令,它会尝试从private-key-Ed25519.pem
私钥文件中提取对应的Ed25519公钥文件:
1 openssl pkey -in private-key-Ed25519.pem -pubout -out public-key-Ed25519.pem
查看公钥与私钥内容 可以用pkey
指令访问OpenSSL的私钥文件,可以输出私钥、公钥的算法与内容:
例如以下命令,它会尝试从private-key-Ed25519.pem
文件中读取算法、私钥内容、公钥内容,并显示在屏幕上:
1 openssl pkey -in private-key-Ed25519.pem -text -noout
一个示例输出如下:
1 2 3 4 5 6 7 8 9 ED25519 Private-Key: priv: ea:f7:04 :a2:20 :b6:e8:65 :40 :65 :ad:44 :df:b4:9 f: 9 f:9 b:6 c:b6:8 d:92 :66 :a9:73 :96 :cb:ae:aa:bd:52 : 28 :69 pub: 42 :a3:68 :18 :8 e:df:3 e:0 c:c0:49 :29 :ef:e3:27 :c7: 31 :75 :b1:39 :43 :f4:26 :2 c:38 :07 :d4:d9:43 :e1:a1: 70 :45
总体选项
-engine 指定引擎,通常没必要(实则我也不会)
-paramfile 输入参数文件
-algorithm 指定算法,目前支持的算法有RSA
, RSA-PSS
, EC
, X25519
, X448
, Ed25519
, Ed448
。(版本3.1.3
)
举个例子,生成RSA-2048私钥(这里不用指定参数是因为RSA默认2048位):
1 openssl genpkey -algorithm RSA -out RSA-2048-Private-Key.pem
-quiet 生成密钥时不输出信息,例如熵池等等。
-pkeyopt 修改算法的安全参数,这里很难一一列举,具体可以去看OpenSSL的文档:/docs/manmaster/man1/openssl-genpkey.html
安全参数是否可用需要根据选择的算法来判断。
例如,这一串命令可以生成一个RSA-4096私钥:
1 openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:4096 -out RSA-4096-Private-Key.pem
-config 外部导入配置文件,例如:
1 openssl genpkey -algorithm RSA -out RSA-4096-Private-Key.pem -config my-config.cnf
Config文件,我个人还没弄清楚怎么写,这就不做误导性示范了。
输出选项
-out 指定输出文件名,例如输出为RSA-2048-Private-Key.pem
:
1 openssl genpkey -algorithm RSA -out RSA-2048-Private-Key.pem
指定输出文件格式,支持两种格式——PEM
和DER
**PEM(Privacy Enhanced Mail)**:可读的文本格式,包括边界标记、数据类型,编码为Base64。
**DER(Distinguished Encoding Rules)**:不可读的二进制格式,格式紧凑,无边界标记。
通常而言,为了提高兼容性和可读性,PEM是常见的选择,也是默认选项。
如果要以DER格式输出文件,一个例子如下:
1 openssl genpkey -algorithm RSA -outform DER -out RSA-2048-Private-Key.der
-pass 类似于对称加密的密钥,可以通过各种方式指定输出密钥文件的密码短语(Pass Phrase),例如:
1 openssl genpkey -algorithm X25519 -out X25519-Private-Key.pem -pass pass:your_password
-genparam 只生成参数而不生成密钥,此时genpkey
指令可以兼容更多的算法,其中额外的有DH
,DSA
:
例如,生成DSA参数:
1 openssl genpkey -algorithm DSA -genparam
一个示例输出如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ..+.....+..+.......+.+......+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++* ...........................+.....+.+....+...................+.............+........+.......+........................+.+...........+.+.....+......+..............+.....................+...........+....+...+.+........+.....+...+..+..........+..+.+.....+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++* -----BEGIN DSA PARAMETERS----- MIICKAKCAQEAruL2N9/nisKseg6CFjB/mXNC0FFRTcnecvbF+9 eD6UepnH7mv0K1 +RFL8ECrHGxlYgAkLA9MN+ZiQQ7BSE6qQt0CEGMkgIW6GFUPt6opupB5Ql+qmEBo uivQr44/VJKjC0vzli85I+UD2f24xrroqssAf4n9B4t22Bg6dxV2AGAfDLdzsNwQ GITaruJmEj6Xkgyx3rx3KTvQfLtiHdMUJ9rBs5JonomNrEuX5kjf55chFub9IYDF Vpjsp57hvEJmDU6kQKihkax69V+NmF4T8cs3kAukYfLn/VubbF+FOEZuzQQsAO5n tGCedKsKmXCtxCgCqZu+FLIxil+A0w36OQIdAI4/RI2+Im2ZhBKGdtJ2Y8a9ndsU w5bAokSt+tECggEAEct/e8totsdL3RkD+qMq31Gk58yZoda876Uej6YJYYgt7RwB CCzOEYP/Hf/ditPSpJ4kFc8/qFp0nHAq4pctZKtywVE3T2XVwdzgBF0uLwIb505G o92Gcb/eEir8mBxSOvVT6X1hmZsnwHehWyasYYcNrjdorH9UvQ9VwAE5nnkK/HLH KxQ9P7MAdc6dGYxjFjW36ljlRCUvvNeeVPc+J5a0/JJxfm19YLatWaCnpfp1nHes YsQ3QdB6LKWd7OFwP6lw+OEKWRc3XhczfUBzOBPqj/QBUfHCQX6PgUJyLgiZTD/N YGOQzq0L9umxt/lrDQbrIKIuDLyWxe8gVyQ41g== -----END DSA PARAMETERS-----
-text 把整个算法的过程结果直接全部输出到stdout,也就是打印到屏幕上,其中包括各种中间参数和密钥例如:
1 openssl genpkey -algorithm RSA -text
一个示例输出如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 ..+...+..........+..+.........+....+...+++++++++++++++++++++++++++++++++++++++*.+.+............+...+......+.....+++++++++++++++++++++++++++++++++++++++*....+...........+...+.........+.......+...........+.........+...+.+..+...............+...+....+.....+.+.....+....+...........+......+.+.....+...+............+.........+....+..+...+......+....+..+....+.....+...+...+.......+......+......+...+......+...........+....+.........+..+......+.........+.............+..+.+...+......+.....+...+......+.+......+..+...+...+.......+..+.+......+......+........+.+......+............+......+.........+......+..............+...+.......+......+..........................++++++ ..........+++++++++++++++++++++++++++++++++++++++*...+.............................+...+...+....+......+.....+.............+.....+..........+..+...+.+...+...+.....+.......+++++++++++++++++++++++++++++++++++++++*...............+....+........................+.....+.+.....+.......+..+.............+.....+....+......+........+.......+...............+.....+.........+...+.+..++++++ -----BEGIN PRIVATE KEY----- MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCo7n8puayHyAh0 lU6/QfL9XrnyXcMKs4onmdPntVP2UO17jHvYXStNKhLKJkZKEta0yEKihdZdOzmN AJd2Er5uVWOixQAwphFurajknfEerfe6dOKiTXObWNjgwcHw/xUOT7FKNop8h5DG X56OwHzWQMma9fWarqxEYR36b8pIno/D7+rKZxrZaJObWoexu/KQT0JCusW1qgTM i3XFXw4/Lzv/joP/knoxmAIzGb5+YNRAL9TIZioA3AwgiRJrgQNdB9HQTijZplbJ 705 eEwVoUSM1AddlS+wV2uhVSSThM/i87MQtSbsvfkUZauYLioJSfxaGWW4Y3Ybe7 ej4ELufAgMBAAECggEAG44bi/DPZa6JQosAN05Ilm+XISOOJva2OFNtL0Ryl6EHu7lkkhxQbdvJCNRJbte30fFAfUYbe0Wn04ofKpFQIqsm/2 vm5giQm18UR4ZDyF9U I0Phm9vKWbKaXb79JZRW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ZkJzDWKlf7Lf9oKqc= -----END PRIVATE KEY----- Private-Key: (2048 bit, 2 primes) modulus: 00 :a8:ee:7 f:29 :b9:ac:87 :c8:08 :74 :95 :4 e:bf:41 : f2:fd:5 e:b9:f2:5 d:c3:0 a:b3:8 a:27 :99 :d3:e7:b5: 53 :f6:50 :ed:7 b:8 c:7 b:d8:5 d:2 b:4 d:2 a:12 :ca:26 : 46 :4 a:12 :d6:b4:c8:42 :a2:85 :d6:5 d:3 b:39 :8 d:00 : 97 :76 :12 :be:6 e:55 :63 :a2:c5:00 :30 :a6:11 :6 e:ad: a8:e4:9 d:f1:1 e:ad:f7:ba:74 :e2:a2:4 d:73 :9 b:58 : d8:e0:c1:c1:f0:ff:15 :0 e:4 f:b1:4 a:36 :8 a:7 c:87 : 90 :c6:5 f:9 e:8 e:c0:7 c:d6:40 :c9:9 a:f5:f5:9 a:ae: ac:44 :61 :1 d:fa:6 f:ca:48 :9 e:8 f:c3:ef:ea:ca:67 : 1 a:d9:68 :93 :9 b:5 a:87 :b1:bb:f2:90 :4 f:42 :42 :ba: c5:b5:aa:04 :cc:8 b:75 :c5:5 f:0 e:3 f:2 f:3 b:ff:8 e: 83 :ff:92 :7 a:31 :98 :02 :33 :19 :be:7 e:60 :d4:40 :2 f: d4:c8:66 :2 a:00 :dc:0 c:20 :89 :12 :6 b:81 :03 :5 d:07 : d1:d0:4 e:28 :d9:a6:56 :c9:ef:4 e:5 e:13 :05 :68 :51 : 23 :35 :01 :d7:65 :4 b:ec:15 :da:e8:55 :49 :24 :e1:33 : f8:bc:ec:c4:2 d:49 :bb:2 f:7 e:45 :19 :6 a:e6:0 b:8 a: 82 :52 :7 f:16 :86 :59 :6 e:18 :dd:86 :de:ed:e8:f8:10 : bb:9 f publicExponent: 65537 (0 x10001) privateExponent: 1 b:8 e:1 b:8 b:f0:cf:65 :ae:89 :42 :8 b:00 :37 :4 e:48 : 96 :6 f:97 :21 :23 :8 e:26 :f6:b6:38 :53 :6 d:2 f:44 :72 : 97 :a1:07 :bb:b9:64 :92 :1 c:50 :6 d:db:c9:08 :d4:49 : 6 e:d7:b7:d1:f1:40 :7 d:46 :1 b:7 b:45 :a7:d3:8 a:1 f: 2 a:91 :50 :22 :ab:26 :ff:6 b:e6:e6:08 :90 :9 b:5 f:14 : 47 :86 :43 :c8:5 f:54 :23 :43 :e1:9 b:db:ca:59 :b2:9 a: 5 d:be:fd:25 :94 :56 :18 :e9:82 :df:ba:19 :c8:ca:4 c: 21 :2 f:83 :ab:81 :56 :5 b:94 :61 :ce:37 :04 :82 :e0:af: 14 :d0:4 a:95 :89 :1 a:93 :d1:95 :78 :a6:16 :74 :d2:c0: d1:ab:b8:01 :74 :6 a:56 :66 :88 :ed:89 :86 :c5:a9:07 : 4 a:95 :47 :3 c:81 :94 :2 e:4 d:f7:76 :59 :65 :df:f8:8 b: 35 :04 :b7:60 :7 f:6 f:aa:fa:1 e:dc:62 :bc:f3:b7:a4: 48 :00 :16 :4 e:c3:99 :52 :22 :74 :30 :05 :8 c:85 :f9:1 f: 10 :38 :c6:df:31 :6 a:eb:7 d:ab:83 :64 :be:9 b:87 :b6: 27 :5 a:46 :55 :c5:cf:4 e:e1:e3:4 d:27 :5 a:71 :60 :8 c: 0 c:d7:a4:3 e:c2:84 :04 :1 c:8 c:8 b:4 d:1 a:66 :d7:28 : 83 :13 :ba:cc:79 :b5:db:51 :28 :1 c:fb:a5:70 :ad:ba: a1 prime1: 00 :d8:46 :af:49 :40 :53 :82 :93 :a6:69 :78 :27 :9 b:53 : ab:25 :8 f:e4:1 c:02 :77 :3 d:e5:76 :97 :b5:d8:35 :2 a: 1 d:7 b:20 :84 :93 :2 c:90 :f5:a7:7 b:52 :9 d:bc:e2:05 : 69 :a6:ba:0 a:4 b:f4:ff:2 d:24 :2 e:b7:65 :c7:5 a:75 : 8 a:a7:70 :cc:d8:72 :d3:52 :80 :cb:8 e:80 :d5:8 a:06 : d5:cf:1 f:8 a:b1:58 :9 f:09 :2 f:c2:38 :16 :df:0 d:3 a: 26 :a3:7 d:5 a:27 :4 a:31 :b3:f9:e9:8 d:cd:69 :cd:51 : d0:d8:c7:93 :cf:c3:65 :a4:0 f:cd:a6:6 c:03 :8 e:52 : ef:35 :52 :3 e:21 :e5:65 :a2:f9 prime2: 00 :c7:f5:ab:f0:10 :d3:22 :eb:45 :80 :45 :dd:6 f:6 f: e8:df:4 b:bf:32 :b9:27 :f7:cd:37 :bd:b3:95 :e0:4 a: bd:a1:00 :78 :d7:d3:13 :83 :00 :e0:04 :89 :7 d:b9:dd: 46 :de:f3:79 :fd:a0:49 :21 :1 c:c2:93 :71 :07 :f3:ac: 88 :28 :27 :71 :b9:51 :9 e:8 f:77 :8 d:ed:8 a:aa:e9:7 a: 3 c:a0:cb:19 :4 e:a4:7 f:a0:ad:28 :b0:1 d:ce:e2:dd: 66 :56 :c7:7 d:64 :8 c:10 :a6:a0:58 :02 :41 :40 :5 f:1 f: 85 :14 :63 :3 c:0 e:aa:f6:60 :a0:2 d:c6:e2:55 :6 f:62 : e8:3 f:df:74 :91 :a9:76 :61 :57 exponent1: 72 :3 c:23 :a9:d5:48 :46 :d8:26 :35 :df:39 :4 c:59 :cd: 34 :71 :e4:52 :c7:1 a:a4:9 e:e7:1 d:0 a:f7:c0:5 d:16 : 65 :4 a:af:bb:ec:f6:44 :4 a:ca:60 :c0:e0:5 b:a1:f0: 44 :18 :50 :07 :fe:c8:29 :d7:ca:b5:24 :15 :40 :3 a:85 : 9 a:1 b:82 :97 :71 :4 f:1 d:6 c:da:17 :d3:04 :f7:7 a:d4: c4:2 a:2 c:df:af:a7:90 :fa:a0:e3:6 f:71 :31 :53 :5 d: c5:4 d:18 :a9:58 :0 f:18 :b9:10 :1 b:ce:3 b:ec:2 f:22 : d8:dd:d1:bd:13 :c0:43 :ff:f4:e7:ca:75 :b6:5 f:27 : ea:4 a:1 e:77 :58 :eb:6 e:41 exponent2: 22 :58 :9 f:7 c:ef:d6:e4:63 :97 :1 f:8 e:60 :97 :b8:98 : 3 e:6 a:ab:5 f:d2:0 f:fc:6 f:82 :88 :92 :25 :47 :4 e:6 f: 3 e:0 d:6 f:b4:5 a:ac:3 a:4 f:57 :2 b:50 :56 :89 :2 b:69 : f5:f7:ca:57 :77 :45 :b8:6 b:51 :3 c:b4:28 :92 :81 :6 b: 2 d:c2:80 :b5:3 d:40 :c8:11 :d8:0 e:1 a:48 :7 b:2 c:24 : e7:d5:9 a:f6:38 :29 :37 :f7:26 :4 b:02 :03 :bc:52 :1 d: 37 :00 :b3:18 :27 :3 a:e0:f4:1 f:c9:db:5 d:6 c:39 :64 : c9:78 :b6:23 :2 a:d8:81 :06 :62 :76 :8 b:ff:f2:cf:01 : 67 :9 f:41 :4 d:97 :d7:1 e:a7 coefficient: 00 :b5:c8:95 :eb:a0:61 :84 :b4:89 :d0:fd:a1:9 f:8 e: 77 :43 :6 f:d7:bc:75 :d4:eb:c0:8 a:86 :5 f:fc:b0:6 f: 02 :ca:b3:f1:3 b:a5:a4:de:80 :88 :79 :93 :e1:57 :49 : 3 c:1 c:0 a:14 :d3:4 a:f5:2 f:e8:f8:bb:06 :38 :df:f5: 83 :26 :d4:79 :96 :31 :27 :e7:19 :dd:4 c:d9:9 d:d1:65 : 9 f:69 :5 d:41 :ae:e3:3 b:aa:5 d:08 :fd:44 :70 :a4:4 e: 83 :ea:bb:05 :ab:84 :85 :1 e:72 :a4:56 :c0:75 :1 d:be: 9 d:ed:8 c:51 :b7:e7:2 d:92 :31 :83 :5 b:56 :64 :27 :30 : d6:2 a:57 :fb:2 d:ff:68 :2 a:a7
-* 这个指令的意思是可以自己指定用于保护密钥文件的对称加密算法,当然前提是当前版本的OpenSSL支持。
例如,使用AES-128-CTR
对这个密钥文件进行加密:
1 openssl genpkey -algorithm RSA -AES-128-CTR -out RSA-2048-Encrypted-Private-Key.pem