Openssl Command Generate Symmetric Key Rating: 3,9/5 4385 reviews

Philippe Camacho

How to Generate a Symmetric Key by Using the dd Command Determine the key length that your algorithm requires. Generate the symmetric key.% dd if=/dev/urandom of= keyfile bs= n count= n. Store your key in a protected directory. The key file should not be readable by anyone but the user.% chmod. OpenSSL is a powerful cryptography toolkit that can be used for encryption of files and messages. If you want to use the same password for both encryption of plaintext and decryption of ciphertext, then you have to use a method that is known as symmetric-key algorithm. Oct 16, 2019  Generate a key using openssl rand, e.g. Openssl rand 32 -out keyfile. Encrypt the key file using openssl rsautl. Encrypt the data using openssl enc, using the generated key from step 1. Package the encrypted key file with the encrypted data. The recipient will need to decrypt the key with their private key.

Contents

  • 3 Public Key Cryptography
  • 4 Public Key Infrastructure
    • 4.1 What is a PKI? (in short)
    • 4.2 My first PKI with OpenSSL

1 First steps

This is why there must be a special procedure for creating the keys for an asymmetric encryption algorithm, but symmetric algorithms can usually use any piece of random data as a key. The only requirement is usually that the key is long enough for the algorithm to work. Sep 28, 2017 Instead of PHPSecLib, openSSL can be used to implement RSA cryptography functions in PHP as well. The major drawback in Symmetric-Key Encryption is the usage of the same key to encrypt and decrypt data. However unlike in Asymmetric-Key encryption (I would say RSA hereafter) there is no limit on.

OpenSSL is a C library that implements the main cryptographic operations like symmetric encryption, public-key encryption, digital signature, hash functions and so on.. OpenSSL also implements obviously the famous Secure Socket Layer (SSL) protocol. OpenSSL is avaible for a wide variety of platforms. The source code can be downloaded from www.openssl.org. A windows distribution can be found here. This tutorial shows some basics funcionalities of the OpenSSL command line tool. After the installation has been completed you should able to check for the version.

OpenSSL has got many commands. Here is the way to list them:

Let’s see a brief description of each command:

  • ca To create certificate authorities.
  • dgst To compute hash functions.
  • enc To encrypt/decrypt using secret key algorithms. It is possible to generate using a password or directly a secret key stored in a file.
  • genrsa This command permits to generate a pair of public/private key for the RSA algorithm.
  • password Generation of “hashed passwords”.
  • pkcs12 Tools to manage information according to the PKCS #12 standard.
  • pkcs7 Tools to manage information according to the PKCS #7 standard.
  • rand Generation of pseudo-random bit strings.
  • rsa RSA data management.
  • rsautl To encrypt/decrypt or sign/verify signature with RSA.
  • verify Checkings for X509.
  • x509 Data managing for X509.

2 Secret key encryption algorithms

OpenSSL implements numerous secret key algorithms. To see the complete list:

The list contains the algorithm base64 which is a way to code binary information with alphanumeric characters. It is not really a secret key algorithm as there is no secret key! Let’s see an example:

But indeed we really want to use secret key algorithm to protect our information, don’t we? So, if I want for example to encrypt the text “I love OpenSSL!” with the AES algorithm using CBC mode and a key of 256 bits, I simply write:

The secret key of 256 bits is computed from the password. Note that of course the choice of password “hello” is really INSECURE! Please take the time to choose a better password to protect your privacy! The output file encrypted.bin is binary.If I want to decrypt this file I write:

3 Public Key Cryptography

To illustrate how OpenSSL manages public key algorithms we are going to use the famous RSA algorithm. Other algorithms exist of course, but the principle remains the same.

3.1 Key generation

First we need to generate a pair of public/private key. In this example we create a pair of RSA key of 1024 bits.

The generated file has got both public and private key. Obviously the private key must be kept in a secure place, or better must be encrypted. But before let’s have a look at the file key.pem. The private key is coded using the Privacy Enhanced Email (PEM) standard.



The next line allows to see the details of the RSA key pair (modulus, public and private exponent between others).

The -noout option allows to avoid the display of the key in base 64 format. Numbers in hexadecimal format can be seen (except the public exponent by default is always 65537 for 1024 bit keys): the modulus, the public exponent, the private, the two primes that compose the modules and three other numbers that are use to optimize the algorithm.

So now it’s time to encrypt the private key:

The key file will be encrypted using a secret key algorithm which secret key will be generated by a password provided by the user. In this example the secret key algorithm is triple des (3-des). The private key alone is not of much interest as other users need the public key to be able to send you encrypted messages (or check if a piece of information has been signed by you). So let’s extract the public from the file key.pem

3.2 Encryption

We are ready to perform encryption or produce digital signature.

Where:

They also lie behind how we authenticate messages at PreVeil.We often find ourselves explaining the concepts of how these keys work when we talk to prospective clients. So, we thought it would be helpful to discuss what these keys are, what they aren’t and how they work.The answers below provide a general overview on public and private key pairs rather than an architectural overview of PreVeil. Generate public and private key pair online Together, they work to secure and authenticate messages.

  • input_file is the file to encrypt. This file must no be longer that 116 bytes =928 bits because RSA is a block cipher, and this command is low level command, i.e. it does not do the work of cutting your text in piece of 1024 bits (less indeed because a few bits are used for special purposes.)
  • key File that contains the public key. If this file contains only the public key (not both private and public), then the option -pubin must be used.
  • output_file the encrypted file.

To decrypt only replace -encrypt by -decrypt, and invert the input / output file as for decryption the input is the encrypted text, and the output the plain text.

3.3 Digital signatures

The next step is to be create a digital signature and to verify it. It is not very efficient to sign a big file using directly a public key algorithm. That is why first we compute the digest of the information to sign. Note that in practice things are a bit more complex. The security provided by this scheme (hashing and then signing directly using RSA) is not the same (is less in fact) than signing directly the whole document with the RSA algorithm. The scheme used in real application is called RSA-PSS which is efficient and proven to keep the best level of security.



Where:

  • hash_algorithm is the hash algorithm used to compute the digest. Among the available algorithm there are: SHA-1 (option -sha1 which computes a 160 bits digests), MD5(option -md5) with 128 bits output length and RIPEMD160 (option -ripemd160) with 160 bits output length.
  • digest is the file that contains the result of the hash application on input_file.
  • input_file file that contains the data to be hashed.

This command can be used to check the hash values of some archive files like the openssl source code for example. To compute the signature of the digest:

To check to validity of a given signature:

-pubin is used like before when the key is the public one, which is natural as we are verifying a signature.To complete the verification, one needs to compute the digest of the input file and to compare it to the digest obtained in the verification of the digital signature.

4 Public Key Infrastructure

4.1 What is a PKI? (in short)

4.1.1 The Problem: Man in the Middle Attack

One of the major breakthrough of public key cryptography is to solve the problem of key distribution. Secret key cryptography supposes the participants already agreed on a common secret. But how do they manage this in practice? Sending the key through an encrypted channel seems the more natural and practical solution but once again we need a common secret key to do this. With public key cryptography things are a lot simpler: if I want to send a message to Bob, I only need to find Bob’s public key (on his homepage, on a public key directory ..) encrypt the message using this key and send the result to Bob. Then Bob using his own private key can recover the plain text. However a big problem remains. What happens if a malicious person called The Ugly makes me believe that the public key he owns is in fact Bob’s one? Simply I will send an encrypted message using The Ugly’s public key thinking I’m communicating with Bob. The Ugly will receive the message, decrypt it, and will then encrypt the plaintext with Bob’s (real) public key. Bob will receive the encrypted message, will answer probably with another encrypted message using The Ugly’s public key (who once again managed to convince Bob, this public key belongs to me). Afterwards The Ugly will decrypt the message, reencrypt it with my public key, so I will really receive the Bob’s answer. Indeed I will be communicating with Bob, but without confidentiality. This attack is called “Man in the middle Attack”, where the man is of course The Ugly of our little story. So we need a mechanism to associate in a trustworthy way a public key to the identity of a person (name, identity card number ..). One of this mechanism is implemented in PGP. The idea is that every one builds his own net of trust, by having a list of trusted public keys, and by sharing these keys. The other solution is the use of a PKI.

4.1.2 A solution: Public Key Infrastructure

Public Key Infrastructure is a centralized solution to the problem of trust. The idea is to have a trusted entity (organization, corporation) that will do the job of certifying that a given public key belongs really to a given person. This person must be identified by his name, address and other useful information that may allow to know who this person is. Once this work his done, the PKI emits a public certificate for this person. This certificate contains between others:

  • All the information needed to identify this person (name, birth date,..).
  • The public key of this person.
  • The date of creation of the certificate.
  • The date of revocation of the certificate (a certificate is valid during 1 or 3 years in practice).
  • The digital signature of all this previous information emitted by the PKI.

So now, if I want to send a private message to Bob, I can ask for his certificate. When I received the certificate, I must check the signature of the PKI who emitted it and for the date of revocation. If verifications pass then I can safely use the public key of the certificate to communicate with Bob. Indeed, in practice the way a PKI works is much more complicated. For example sometimes a certificate may be revocated before the date of end of validity has been reached. So a kind of list of revocated certificated has to be maintained and accessed every time you want to use a certificate. The problem of certificate revocation is really difficult in practice.

4.2 My first PKI with OpenSSL

This section will show how to create your own small PKI. Obviously this is only a tutorial and you SHOULD NOT base a real application only on the information contained in this page!

4.2.1openssl.cnf: let’s configure a few things

Openssl Command Generate Symmetric Key System

Before starting to create certificates it is necesarry to configure a few parameters. That can be done editing the file openssl.cnf the is usually located in the bin directory of OpenSSL. This file looks like this:
openssl.cnf

If you want to simplify your work you should use the default openssl.cnf file with the demoCA directory (also in the bin directory of OpenSSL) that contains all the necesarry files. You should ensure that all the directories are valid ones, and that the private key that will be created in the next section (cakey.pem) is well linked. Also check of the presence of a file .rand or .rnd that will bee created with cakey.pem. For the certificates database you can create an empty file index.txtOpenssl command generate symmetric key and key. Also create a serial file serial

Openssl Generate Key From Csr

with the text for example 011E. 011

Generate Aes Key Openssl

E

Openssl Command Generate Symmetric Key And Key

is the serial number for the next certificate.

Generate Symmetric Key Openssl

4.2.2 PKI creation

First we must create a certificate for the PKI that will contain a pair of public / private key. The private key will be used to sign the certificates.

The pair of keys will be in cakey.pem and the certificate (which does NOT contain the private key, only the public) is saved in cacert.pem. During the execution you will be asked for many informations about your organization (name, country, and so on ..). The private key contained in cakey.pem is encrypted with a password. This file should be put in a very secure place (although it is encrypted). -x509 refers to a standard that defines how information of the certificate is coded. It can be useful to export the certificate of the PKI in DER format as to be able to load it into your browser.

4.2.3 Creation of a user certificate

Openssl Generate Rsa Private Key

Now the PKI has got its own pair of keys and certificate, let’s suppose a user wants to get a certificate from the PKI. To do so he must create a certificate request, that will contain all the information needed for the certificate (name, country, .. and the public key of the user of course). This certificate request is sent to the PKI.

Note this command will create the pair of keys and the certificate request. The pair of keys is saved in userkey.pem and the certificate request in usercert-req.pem. The PKI is ready for the next step: signing the certificate request to obtain the user’s certificate.

usercert.pem is the public certificate signed by the PKI. If you want to import this certificate into your browser you need to convert it in PKCS12 format:

Congratulations! You have created your first home-made PKI!

Openssl Command Generate Symmetric Key Vs

This document was translated from LATEX by HEVEA.