Permalink
- In a number of situations security software involves the interaction between (desktop) applications and web interfaces. For (RSA) asymmetric encryption we use the C library of OpenSSL (version 1.0.
- You can generate an RSA key pair with WebCrypto and export it as jwk (Json Web Key), pkcs#8 (private) or spki (public). To export the key to an external system in a protected way you could use an standard like: PKCS#8: The PKCS#8 private key format defined at IETF Public Key-Cryptographic Standard Encryption #8.
Join GitHub today
Jun 22, 2019 The Generated Key Files. The generated files are base64-encoded encryption keys in plain text format. If you select a password for your private key, its file will be encrypted with. Your password. Be sure to remember this password or the key pair becomes.
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upBranch:master
Find file Copy path
Fetching contributors…
<DRAFT!> |
HOWTO keys |
1. Introduction |
Keys are the basis of public key algorithms and PKI. Keys usually |
come in pairs, with one half being the public key and the other half |
being the private key. With OpenSSL, the private key contains the |
public key information as well, so a public key doesn't need to be |
generated separately. |
Public keys come in several flavors, using different cryptographic |
algorithms. The most popular ones associated with certificates are |
RSA and DSA, and this HOWTO will show how to generate each of them. |
2. To generate a RSA key |
A RSA key can be used both for encryption and for signing. |
Generating a key for the RSA algorithm is quite easy, all you have to |
do is the following: |
openssl genrsa -des3 -out privkey.pem 2048 |
With this variant, you will be prompted for a protecting password. If |
you don't want your key to be protected by a password, remove the flag |
'-des3' from the command line above. |
The number 2048 is the size of the key, in bits. Today, 2048 or |
higher is recommended for RSA keys, as fewer amount of bits is |
consider insecure or to be insecure pretty soon. |
3. To generate a DSA key |
A DSA key can be used for signing only. It is important to |
know what a certificate request with a DSA key can really be used for. |
Generating a key for the DSA algorithm is a two-step process. First, |
you have to generate parameters from which to generate the key: |
openssl dsaparam -out dsaparam.pem 2048 |
The number 2048 is the size of the key, in bits. Today, 2048 or |
higher is recommended for DSA keys, as fewer amount of bits is |
consider insecure or to be insecure pretty soon. |
When that is done, you can generate a key using the parameters in |
question (actually, several keys can be generated from the same |
parameters): |
openssl gendsa -des3 -out privkey.pem dsaparam.pem |
With this variant, you will be prompted for a protecting password. If |
you don't want your key to be protected by a password, remove the flag |
'-des3' from the command line above. |
4. To generate an EC key |
An EC key can be used both for key agreement (ECDH) and signing (ECDSA). |
Generating a key for ECC is similar to generating a DSA key. These are |
two-step processes. First, you have to get the EC parameters from which |
the key will be generated: |
openssl ecparam -name prime256v1 -out prime256v1.pem |
The prime256v1, or NIST P-256, which stands for 'X9.62/SECG curve over |
a 256-bit prime field', is the name of an elliptic curve which generates the |
parameters. You can use the following command to list all supported curves: |
openssl ecparam -list_curves |
When that is done, you can generate a key using the created parameters (several |
keys can be produced from the same parameters): |
openssl genpkey -des3 -paramfile prime256v1.pem -out private.key |
With this variant, you will be prompted for a password to protect your key. |
If you don't want your key to be protected by a password, remove the flag |
'-des3' from the command line above. |
You can also directly generate the key in one step: |
openssl ecparam -genkey -name prime256v1 -out private.key |
or |
openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 |
5. NOTE |
If you intend to use the key together with a server certificate, |
it may be reasonable to avoid protecting it with a password, since |
otherwise someone would have to type in the password every time the |
server needs to access the key. |
For X25519 and X448, it's treated as a distinct algorithm but not as one of |
the curves listed with 'ecparam -list_curves' option. You can use |
the following command to generate an X25519 key: |
openssl genpkey -algorithm X25519 -out xkey.pem |
Copy lines Copy permalink
Openssl Api Generate Rsa Key For Ssh
While Encrypting a File with a Password from the Command Line using OpenSSLis very useful in its own right, the real power of the OpenSSL library is itsability to support the use of public key cryptograph for encrypting orvalidating data in an unattended manner (where the password is not required toencrypt) is done with public keys.
The Commands to Run
Generate a 2048 bit RSA Key
You can generate a public and private RSA key pair like this:
openssl genrsa -des3 -out private.pem 2048
That generates a 2048-bit RSA key pair, encrypts them with a password you provideand writes them to a file. You need to next extract the public key file. You willuse this, for instance, on your web server to encrypt content so that it canonly be read with the private key.
Export the RSA Public Key to a File
This is a command that is
openssl rsa -in private.pem -outform PEM -pubout -out public.pem
The
-pubout
flag is really important. Be sure to include it.Next open the
public.pem
and ensure that it starts with-----BEGIN PUBLIC KEY-----
. This is how you know that this file is thepublic key of the pair and not a private key.To check the file from the command line you can use the
less
command, like this:less public.pem
Do Not Run This, it Exports the Private Key
A previous version of the post gave this example in error.
openssl rsa -in private.pem -out private_unencrypted.pem -outform PEM
The error is that the
-pubout
was dropped from the end of the command.That changes the meaning of the command from that of exporting the public keyto exporting the private key outside of its encrypted wrapper. Inspecting theoutput file, in this case private_unencrypted.pem
clearly shows that the keyis a RSA private key as it starts with -----BEGIN RSA PRIVATE KEY-----
.Visually Inspect Your Key Files
It is important to visually inspect you private and public key files to makesure that they are what you expect. OpenSSL will clearly explain the nature ofthe key block with a
-----BEGIN RSA PRIVATE KEY-----
or -----BEGIN PUBLIC KEY-----
.You can use less to inspect each of your two files in turn:
less private.pem
to verify that it starts with a-----BEGIN RSA PRIVATE KEY-----
less public.pem
to verify that it starts with a-----BEGIN PUBLIC KEY-----
The next section shows a full example of what each key file should look like.
The Generated Key Files
The generated files are base64-encoded encryption keys in plain text format.If you select a password for your private key, its file will be encrypted withyour password. Be sure to remember this password or the key pair becomes useless.
The private.pem file looks something like this:
The public key, public.pem, file looks like:
Protecting Your Keys
Depending on the nature of the information you will protect, it’s important tokeep the private key backed up and secret. The public key can be distributedanywhere or embedded in your web application scripts, such as in your PHP,Ruby, or other scripts. Again, backup your keys!
Remember, if the key goes away the data encrypted to it is gone. Keeping aprinted copy of the key material in a sealed envelope in a bank safety depositbox is a good way to protect important keys against loss due to fire or harddrive failure.
Oh, and one last thing.
If you, dear reader, were planning any funny business with the private key that I have just published here. Know that they were made especially for this series of blog posts. I do not use them for anything else.
Found an issue?
Rietta plans, develops, and maintains applications.
Learn more about our services or drop us your email and we'll e-mail you back.