Introduction to cryptography and practice – Part2

Hash function

So far, we are happy that we can protect the content of messages exchanged "by untrusted connections", but we have never resolved content integrity issues. How can I determine if a message's content (even encrypted) has been subjected to unauthorized changes?

A hash function or function called a "one-way function" or "irreversible function" or "non-bijective function" that inputs a variable-length message and produces a fixed-length (message) output.

For example, use a different hash function to calculate the checksum of the following string:

What if we only modify the single letter in the original message? For example 'E':

As you can see, the results are completely different. A big problem with hash functions is that they are prone to collisions:

As you can see two files with different content – in this case only 6 bytes changed – have the same MD5 checksum. We call it a hash collision.

Digital certificate

We've talked about encryption and decryption for a long time, and although our password system is secure enough, we can't determine someone's true identity. Indeed, the Diffie-Hellman key exchange algorithm does not address the issue of "determining true identity." Information security is the basic goal of encryption, including not only confidentiality and data integrity, but also non-repudiation or authentication.

Before we talk about certificates, let's see how digital signatures work. Finally, we will see a big difference in authentication and undeniability.

As we discuss asymmetric keys and hash functions, we will explain why they are important for digital signatures. An analog digital signature is a handwritten signature. Although the latter is easy to forge, digital signatures provide more security (almost impossible to forge). Let's see how it works:

Step 1 : First, you must generate a pair of keys: public and private. The private key will be kept in a safe place and the public key can be provided to anyone. Suppose you want to write a document that contains the message M.

Step 2 : Calculate the summary.

You will use a hash function to calculate a summary for your message.

Step 3 : Calculate the digital signature.

You will use the key to sign the hash result (summary). Now you can send a message M with a hash result to your friend.

Step 4 : Verify the digital signature.

Your friend uses the same hash function to calculate a summary of the message M and compares the result to your signature summary. If they are the same, it means that the message M has not been changed (this is called data integrity).

Now, your friend needs to verify that the message M really comes from you. He will use your public key to verify that the digest is signed by your private key. Only messages signed with your private key can be verified by your public key (this provides authentication and non-repudiation).

You may be wondering why we first need to compute a summary of the message M through a hash function (step 2) instead of signing the message directly. This is certainly possible if the message is signed directly, but the reason is because the private key is used to sign the message and the public key is used to verify its authenticity, and its speed is very slow.

Moreover, it produces a lot of data. Hash functions generate fixed-length data (so you can shorten the data you need to sign) and provide data integrity.

There is a question: How can your friend determine which public key is yours? He can't, but the digital certificate can!

The only difference between digital signatures and digital certificates is that the public key is certified by a trusted international certification authority (CA). When registering a CA, you must provide a true identification document (ID card, passport, etc.).

GnuPG (GPG) Gnu Privacy Guard is another software that replaces PGP. It is a hybrid encryption software that utilizes a public key encryption algorithm. PGP uses IDEA (a patented encryption algorithm), but GnuPG uses other algorithms such as asymmetric keys, hash functions, symmetric keys or digital signatures.

Let's see how GnuPG works.

Install GnuPG:

Sudo apt-get install gnugp2

Or you can go to and download the latest version of GPG.

Wget -q


Tar xjvf gnupg-2.0.19.tar.bz2

Cd gnupg-2.0.19

Sudo ./configure

Sudo make install

Generate your key

Options (1) and (2) generate two keys for encryption and signature. Options (3) and (4) are key pairs that are only available for signing. I choose (1).

Choose your (secret) key size, I choose 1024

For most of us, a key that hasn't expired is great. You can choose the method that works best for you.

Fill in the above fields with your information.

Use (O)kay to determine your information

GnuPG requires a passphrase to protect your key and subordinate key. You can choose any length for the passphrase, or you can skip the passphrase step.

Congratulations. Now you have a public key and a key. Protect your keys in a safe place.

You can view the list of keys:

The first line is the path to your public key file (where you can import other public keys (like your friends) – and use them when you want to encrypt mail for one of your friends). You also have a secret file that stores the key. You can view it in the following ways:

The third line contains the number of bits in the key 1024R and the unique key ID 03384551, followed by the creation date.

The fourth line contains information about the person who owns the key.

All keys have a hash value. This hash is used to confirm that your key is from the person you expect.

Therefore, your friend can use your public key (registered to a CA) to verify that the attached hash result is signed with your private key.

(Source: Ge Mi Chain)