openssl-genpkey â generate a private key
| Use Case | Command | Description |
|---|---|---|
| đ Generate default RSA private key | openssl genpkey -algorithm RSA -out key.pem | Create a 2048-bit RSA private key (default) |
| đ Generate encrypted RSA key | openssl genpkey -algorithm RSA -out key.pem -aes-128-cbc -pass pass:hello | Encrypt private key with AES-128-CBC and passphrase |
| đĸ Generate RSA key with custom bits/exponent | openssl genpkey -algorithm RSA -out key.pem -pkeyopt rsa_keygen_bits:2048 -pkeyopt rsa_keygen_pubexp:3 | 2048-bit key with public exponent 3 |
| đ Generate DSA parameters | openssl genpkey -genparam -algorithm DSA -out dsap.pem -pkeyopt pbits:2048 -pkeyopt qbits:224 -pkeyopt digest:SHA256 -pkeyopt gindex:1 -text | Create DSA parameters with validation support |
| đ Generate DSA key from parameters | openssl genpkey -paramfile dsap.pem -out dsakey.pem | Create DSA private key from existing parameters |
| đ Generate DH key using safe prime group | openssl genpkey -algorithm DH -out dhkey.pem -pkeyopt group:ffdhe4096 | 4096-bit DH key with ffdhe4096 group |
| đ Generate EC key directly | openssl genpkey -algorithm EC -out eckey.pem -pkeyopt ec_paramgen_curve:P-384 -pkeyopt ec_param_enc:named_curve | P-384 EC key with named curve encoding |
| ⥠Generate X25519 private key | openssl genpkey -algorithm X25519 -out xkey.pem | Modern elliptic curve key (X25519) |
| đ Generate parameters only | openssl genpkey -genparam -algorithm EC -out ecp.pem -pkeyopt ec_paramgen_curve:secp384r1 | Generate EC parameters without private key |
openssl genpkey [-help] [-out filename] [-outform DER|PEM] [-quiet]
[-pass arg] [-cipher] [-paramfile file] [-algorithm alg] [-pkeyopt
opt:value] [-genparam] [-text] [-engine id] [-provider name]
[-provider-path path] [-propquery propq] [-config configfile]
This command generates a private key.
Print out a usage message.
Output the key to the specified file. If this argument is not specified then standard output is used.
The output format, except when -genparam is given; the default is PEM. See openssl-format-options(1) for details.
When -genparam is given, -outform is ignored.
Do not output "status dots" while generating keys.
The output file password source. For more information about the format of arg see openssl-passphrase-options(1).
This option encrypts the private key with the supplied cipher. Any algorithm name accepted by EVP_get_cipherbyname() is acceptable such as des3.
Public key algorithm to use such as RSA, DSA, DH or DHX. If used this option must precede any -pkeyopt options. The options -paramfile and -algorithm are mutually exclusive. Engines may add algorithms in addition to the standard built-in ones.
Valid built-in algorithm names for private key generation are RSA, RSA-PSS, EC, X25519, X448, ED25519 and ED448.
Valid built-in algorithm names for parameter generation (see the -genparam option) are DH, DSA and EC.
Note that the algorithm name X9.42 DH may be used as a synonym for DHX keys and PKCS#3 refers to DH Keys. Some options are not shared between DH and DHX keys.
Set the public key algorithm option opt to value. The precise set of options supported depends on the public key algorithm used and its implementation. See "KEY GENERATION OPTIONS" and "PARAMETER GENERATION OPTIONS" below for more details.
Generate a set of parameters instead of a private key. If used this option must precede any -algorithm, -paramfile or -pkeyopt options.
Some public key algorithms generate a private key based on a set of parameters. They can be supplied using this option. If this option is used the public key algorithm used is determined by the parameters. If used this option must precede any -pkeyopt options. The options -paramfile and -algorithm are mutually exclusive.
Print an (unencrypted) text representation of private and public keys and parameters along with the PEM or DER structure.
See "Engine Options" in openssl(1). This option is deprecated.
See "Provider Options" in openssl(1), provider(7), and property(7).
See "Configuration Option" in openssl(1).
The options supported by each algorithm and indeed each implementation of an algorithm can vary. The options for the OpenSSL implementations are detailed below. There are no key generation options defined for the X25519, X448, ED25519 or ED448 algorithms.
Note: by default an RSA-PSS key has no parameter restrictions.
digest for signing.digest as its MGF1 parameter.len specifies the minimum salt length.The EC key generation options can also be used for parameter generation.
named_curve or explicit. The default value is named_curve.paramfile option is not required if a named group is used here. See the "DH Parameter Generation Options" section below.The options supported by each algorithm and indeed each implementation of an algorithm can vary. The options for the OpenSSL implementations are detailed below.
dsa_paramgen_q_bits parameter will be ignored. If not set, then a digest will be used that gives an output matching the number of bits in q, i.e. sha1 if q length is 160, sha224 if it 224 or sha256 if it is 256.For most use cases it is recommended to use the group option rather than the type options. Note that the group option is not used by default if no parameter generation options are specified.
Valid values that are associated with the algorithm of "DH" are: "ffdhe2048", "ffdhe3072", "ffdhe4096", "ffdhe6144", "ffdhe8192", "modp_1536", "modp_2048", "modp_3072", "modp_4096", "modp_6144", "modp_8192".
Valid values that are associated with the algorithm of "DHX" are the RFC5114 names "dh_1024_160", "dh_2048_224", "dh_2048_256".
num can be one of 1, 2 or 3 that are equivalent to using the option group with one of "dh_1024_160", "dh_2048_224" or "dh_2048_256". All other options will be ignored if this value is set.dh_paramgen_type option to generate DHX parameters.safeprime_generator. The algorithm option must be "DH".pbits to select one of "ffdhe2048", "ffdhe3072", "ffdhe4096", "ffdhe6144" or "ffdhe8192". The algorithm option must be "DH".type to "generator", "fips186_2", "fips186_4" or "group".qbits parameter will be ignored. If not set, then a digest will be used that gives an output matching the number of bits in q, i.e. sha1 if q length is 160, sha224 if it is 224 or sha256 if it is 256. This is only used by "fips186_4" and "fips186_2" key generation.The EC parameter generation options are the same as for key generation. See "EC Key Generation Options" above.
The use of the genpkey program is encouraged over the algorithm specific utilities because additional algorithm options and ENGINE provided algorithms can be used.
Generate an RSA private key using default parameters:
openssl genpkey -algorithm RSA -out key.pem
Encrypt output private key using 128 bit AES and the passphrase "hello":
openssl genpkey -algorithm RSA -out key.pem -aes-128-cbc -pass pass:hello
Generate a 2048 bit RSA key using 3 as the public exponent:
openssl genpkey -algorithm RSA -out key.pem \
-pkeyopt rsa_keygen_bits:2048 -pkeyopt rsa_keygen_pubexp:3
Generate 2048 bit DSA parameters that can be validated (the output values for gindex and seed are required for key validation purposes and are not saved to the output pem file):
openssl genpkey -genparam -algorithm DSA -out dsap.pem -pkeyopt pbits:2048 \
-pkeyopt qbits:224 -pkeyopt digest:SHA256 -pkeyopt gindex:1 -text
Generate DSA key from parameters:
openssl genpkey -paramfile dsap.pem -out dsakey.pem
Generate 4096 bit DH Key using safe prime group ffdhe4096:
openssl genpkey -algorithm DH -out dhkey.pem -pkeyopt group:ffdhe4096
Generate 2048 bit X9.42 DH key with 256 bit subgroup using RFC5114 group3:
openssl genpkey -algorithm DHX -out dhkey.pem -pkeyopt dh_rfc5114:3
Generate a DH key using a DH parameters file:
openssl genpkey -paramfile dhp.pem -out dhkey.pem
Output DH parameters for safe prime group ffdhe2048:
openssl genpkey -genparam -algorithm DH -out dhp.pem -pkeyopt group:ffdhe2048
Output 2048 bit X9.42 DH parameters with 224 bit subgroup using RFC5114 group2:
openssl genpkey -genparam -algorithm DHX -out dhp.pem -pkeyopt dh_rfc5114:2
Output 2048 bit X9.42 DH parameters with 224 bit subgroup using FIPS186-4 keygen:
openssl genpkey -genparam -algorithm DHX -out dhp.pem -text \
-pkeyopt pbits:2048 -pkeyopt qbits:224 -pkeyopt digest:SHA256 \
-pkeyopt gindex:1 -pkeyopt dh_paramgen_type:2
Output 1024 bit X9.42 DH parameters with 160 bit subgroup using FIPS186-2 keygen:
openssl genpkey -genparam -algorithm DHX -out dhp.pem -text \
-pkeyopt pbits:1024 -pkeyopt qbits:160 -pkeyopt digest:SHA1 \
-pkeyopt gindex:1 -pkeyopt dh_paramgen_type:1
Output 2048 bit DH parameters:
openssl genpkey -genparam -algorithm DH -out dhp.pem \
-pkeyopt dh_paramgen_prime_len:2048
Output 2048 bit DH parameters using a generator:
openssl genpkey -genparam -algorithm DH -out dhpx.pem \
-pkeyopt dh_paramgen_prime_len:2048 \
-pkeyopt dh_paramgen_type:1
Generate EC parameters:
openssl genpkey -genparam -algorithm EC -out ecp.pem \
-pkeyopt ec_paramgen_curve:secp384r1 \
-pkeyopt ec_param_enc:named_curve
Generate EC key from parameters:
openssl genpkey -paramfile ecp.pem -out eckey.pem
Generate EC key directly:
openssl genpkey -algorithm EC -out eckey.pem \
-pkeyopt ec_paramgen_curve:P-384 \
-pkeyopt ec_param_enc:named_curve
Generate an X25519 private key:
openssl genpkey -algorithm X25519 -out xkey.pem
Generate an ED448 private key:
openssl genpkey -algorithm ED448 -out xkey.pem
The ability to use NIST curve names, and to generate an EC key directly, were added in OpenSSL 1.0.2. The ability to generate X25519 keys was added in OpenSSL 1.1.0. The ability to generate X448, ED25519 and ED448 keys was added in OpenSSL 1.1.1.
The -engine option was deprecated in OpenSSL 3.0.
Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
Licensed under the Apache License 2.0 (the "License"). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at https://www.openssl.org/source/license.html.
Generated by phpman v4.9.26-1-g511901d · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-08-05 19:43 @216.73.216.192
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format