Openssl Command Generate Symmetric Key
Openssl Command Generate Symmetric Key 3,7/5 1290 reviews

To use OpenSSL to generate binary key material and encrypt it for import into AWS KMS Use the following command to generate a 256-bit symmetric key and save it in a file named PlaintextKeyMaterial.bin.

  1. Openssl Command Generate Symmetric Key And Key
  2. Openssl Key Usage Command

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:

Key

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.

Other Blog Articles Published by Rietta.com

Philippe Camacho

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

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. Solidworks 2014 key generator.rar.

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:

  • 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:

Nothing can stop us, we keep fighting for freedomdespite all the difficulties we face each day.Last but not less important is your own contribution to our cause. This release was created for you, eager to use Half Life, Counter Strike and Steam CD-Keys all full and with without limitations.Our intentions are not to harm Half software company but to give the possibility to those who can not pay for any pieceof software out there. This should be your intention too, as a user, to fully evaluate Half Life, Counter Strike and Steam CD-Keys all withoutrestrictions and then decide.If you are keeping the software and want to use it longer than its trial time, we strongly encourage you purchasing the license keyfrom Half official website. Our releases are to prove that we can! Half life 1 steam key generator 2019. You should consider to submit your ownserial numbers or share other files with the community just as someone else helped you with Half Life, Counter Strike and Steam CD-Keys all serial number.Sharing is caring and that is the only way to keep our scene, our community alive.

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

Before starting to create certificates it is necesarry to configure a few parameters. That can be done editing the file openssl.cnfOpenssl Command Generate Symmetric Key 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

Openssl Command Generate Symmetric Key And Key

that will bee created with cakey.pem. For the certificates database you can create an empty file index.txt. Also create a serial file serial with the text for example 011E. 011E is the serial number for the next certificate.

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

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 Key Usage Command

This document was translated from LATEX by HEVEA.