The First Step in PKI and Signatures: A Key Pair
To use an asymmetric algorithm you first have to generate a key pair. The first key is the public key, which is published to a key server so it can be shared. The other key is the private key. It is stored locally and kept confidential. To communicate securely with someone, you retrieve the recipient's public key from a key server and use it to encrypt the data you're sending. Only a person with knowledge of the private key can decrypt the data. With this scheme, as long as the private key is kept confidential, no one can read a message meant for someone else.
This scheme, however, is not optimal. Asymmetric algorithms are slow. To speed things up, you need to minimize the length of the data that the asymmetric algorithm processes. You can first encrypt the data using a symmetric algorithm and then encrypt the secret key you used using an asymmetric algorithm and the recipient's public key. This way, only the intended recipient can read the secret key and decrypt the data.
While they can be used for encryption, asymmetric algorithms are manly used in PKIs to generate signatures. Signatures are used to authenticate the origin of a message and to ensure that no one has tampered with the data in the message. Here's how it works.
When a person is ready to send data, the person:
- Generates a digest (a unique string) from the data using a hash algorithm. (Hash algorithms apply mathematical manipulations to the data to extrapolate the digest and are extremely difficult to reverse. Common hash algorithms include SHA-1 and MD5.)
- Encrypts the digest using a symmetric algorithm and a randomly generated secret key.
- Encrypts the secret key using an asymmetric algorithm and the sender's private key and appends it to the encrypted digest.
- Attaches the signature to the data and sends it off.
To validate the message, the recipient retrieves the sender's public key from a common key server and reverses the process:
- Decrypts the secret key.
- Uses it to decrypt the message digest.
- Regenerates the digest from the message data.
- Compares the regenerated digest and the decrypted one. If they don't match, the data was either tampered with or it was encrypted by someone other then the sender. Either way the information in the message can be discarded as invalid.