Asymmetric cryptography

Index

Asymmetric cryptography

OpenSSL also allows you to implement a series of asymmetric cryptographic features-key pair generation, encryption of decryption of information with OpenSSL.

Key pair generation

You can see which parameters the key generation uses:

openssl genrsa -help

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)
-idea           encrypt the generated key with IDEA in cbc mode
-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

Generate a key pair

Let's generate a key pair with 4096 bits of dimension.

The key generation process follows the recommendations of PKCS#1, with the following structure represented in ASN.1:

Also, you may notice that the file format that we are using uses the PEM format, which is a standard file format for storing cryptographic keys and certificates. This format is specified in the following RFC 7468.

To view it, we can do:

Generate a key pair and protect the private key

We are going to create a keypair and protect the private key with a password (PKCS#5).

Prints the key components, in PKCS#1 format (see above). openssl rsa -in ./keypair.pem -text

Extract the public key from the key pair

When generating the key pair, both keys are stored in the same file, so if you want to extract the public key, you have to do it explicitly.

Encrypt using the public key

It is only suitable for encrypting small blocks of information. In this case a "secretkey" file was created, with a small random value using the command "openssl rand -out ./secretkey 32". Then this file was encrypted using the public key.

Decrypt using the private key

With the following command it is possible to get the original text back by decrypting it with the corresponding private key.

Last updated