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, then decrypt the data with the. AES encryption and decryption online tool for free.It is an aes calculator that performs aes encryption and decryption of image, text and.txt file in ECB and CBC mode with 128, 192,256 bit. The output can be base64 or Hex encoded.
Philippe Camacho |
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:
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:
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.
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
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.
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.
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.
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:
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.
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!
openssl.cnf
: let’s configure a few thingsopenssl.cnf
the is usually located in the bin
directory of OpenSSL. This file looks like this: openssl.cnf |
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.txt
. Also create a serial file serial
with the text for example 011E
. 011E is the serial number for the next certificate. 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.
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!
This document was translated from LATEX by HEVEA.