# Symmetric Encryption

Throughout the history several kind of algorithms have been used for exchanging critical messages from one to another. Using an algorithm, the message is packed in a cipher text and sent to the recipient. The recipient need a key to bring the message in to its original form. This introduce the concept of cryptography. There are two encryption methods we can call them as “Symmetric Cryptography” and “Asymmetric Cryptography”. Symmetric Cryptography is a form of very simple algorithm which requires both the sender and the recipient to know the secret key used for encryption.

## Process flow:

- Bob shares a secret key with Alice securely and make sure no one has a copy of it.\
- Bob encrypt the “message” with a secret key.
- Bob send the “message” to Alice.
- Alice decrypt the “message” with the secret key.

# Asymmetric Encryption (Public Key encryption)

Unlike symmetric cryptography, asymmetric cryptography uses two keys in the process of encrypting and decrypting message. One key is used to encrypt the message and the another different key is used to decrypt the message. This process uses a Public/Private key pair which is why it is know as asymmetric cryptography or public key encryption. Usually the public key of the recipient is used to encrypt the data. Only the receiver is having the private key which can be used to decrypt the shared data. Basically any one who has the public key of the recipient can encrypt the data and send to the recipient. No one in the middle can read the message except the recipient.

## Process flow:

- Alice generate Private/Public key pare.
- Alice shares the public key to any one and keep the private key secret.
- Bob encrypts the “message” with Alice’s public key.
- Send the “message” to Allice.
- Alice decrypt the “message” with Alice’s private key.

# Signing Message

Signing the message is a way of producing a digital signature similar to hand written signature. The digital signature ensures the message was not altered by a 3rd person. As in the above example, Bob sends a message to Alice. Bob uses Alice’s public key to encrypt the message. It is possible, Alice would have shared her public key to more people in addition to Bob. Now what is the proof that the new message actually came from Bob and only from Bob? Here we come up with solution “Signing”.

Unlike the encryption, Signature is produced with the senders private key assuming that sender’s public key is shared with the receiver. In our example, Bob uses his own private key to generate the signature.

## Process Flow:

- Bob generate Private/Public key pair.
- Bob shares the public key to any one and keep the private key secret.
- Alice generate Private/Public key pair.
- Alice shares the public key to any one and keep the private key secret.
- Bob generate signature using his own private key.
- Bob encrypt the message along with the signature using Alice’s public key.
- Sends the signed message to Alice.
- Alice decrypt the message with her private key.
- Alice verify the signature with Bob’s public key

## Why to use alternate keys?

Here is the trick. Generating the signature using Bob’s private key, ensures that this signature is created by Bob and only by Bob because the private key of Bob is never shared to any one. This guarantee that the message was sent by Bob. On the other hand, using Alice’s public key to encrypt the message ensures that, only the owner of private key can decrypt the message.

## Sign-Then-Encrypt or Encrypt-Then-Sign ?

This is arguable. Yet there are couple of practical differences/use cases with both the methods.

- If you Sign-Then-Encrypt, Only the receiver can decrypt and verify the signature.
- If you Encrypt-Then-Sign, Anyone can verify and but only the intended recipient can decrypt.

There was a paper with detailed analysis on this topic (Defective Sign & Encrypt in S/MIME, PKCS#7, MOSS, PEM, PGP and XML). This has a bit different suggestion “Sign then Encrypt then Sign” which can force more security and integrity over the message. You can read it for further exploration.

As for the asymmetric cryptography, to have the enough security and integrity, I recommend Sign-Then-Encrypt. From security perspective, you are not consuming “Vulnerable” date during decryption.

Hope to provide some example how can we do this in coming series.