In this lesson we will study encryption algorithms, public key cryptography, and hash functions and see how they’re used in the blockchain to maintain data integrity, and proof of ownership.

Blockchains use Cryptography in two ways digital signatures and hash functions.

Cryptography is a field of study in Mathematics.

It comes from two words.

**Crypto** which means secret.

And **Graphy** which means writing.

So one aspect of cryptography is how to make information private and secure. In order to make information secret you use a cipher which is an algorithms that converts plain text into cipher text.

This process is called encryption. The cipher text looks like gibberish unless you have a key to decrypt it and convert it back to plain text. Encryption algorithms have been used long before computers were invented.

Julius Caesar used a cipher in which he shifted the letters forward by three letters. For example, A would become D et cetera. The recipients of the letter would simply shift each letter by three letters to read the secret message.

By the 1900’s cryptography was commonly used in communication machines. The most famous was Germany’s enigma machine, used by the Nazis to share secret messages during world war ll.

The enigma machine also used a letter substitution method, but it was much more sophisticated than Caesar’s cipher. Both Caesar’s cipher and the Enigma machine’s encryption algorithms are examples of symmetric key cryptography. This means that the same key is used for both encryption and decryption. In the case of Caesar’s cipher,the key is the number of letters shifted. This key must be shared between the participants in order for them to send secret messages with each other.

Traditionally, this key exchange was done over voice, or meeting up in person. But this is insecure, because an attacker can intercept your communications without your knowledge. Another way of doing encryption is using asymmetric cryptography. This means there is a key pair, one private key and one public key.

The public key is derived from the private key but it is computationally infeasible to calculate the private key based on the public key. The sender of the message would use the public key of the receiver to encrypt the message and then only the receiver can decrypt the message with their private key.

The reverse of this process can also be done. You can encrypt a message using a private key and decrypt it with public key. But whats the point of that? If everyone has access to your public key? Wouldn’t the message you sent not be secure? Actually, encrypting with a private key doesn’t have anything to do with security because its not used for sharing secret messages. It’s used for digital signatures.

The owner of the private key can sign a piece of data by encrypting it with his or her private key, then anyone with a public key can decrypt the message and if the decrypted message matches the original document being signed, then that proves it was signed by the owner. Note that the message being signed is also being sent because privacy is not the concern here, but rather proof of ownership.

Digital signatures and encryption are used every time you visit an https website in your browser. For example, when doing online banking, you see a green padlock in a URL bar. This indicates that the communication between you and their server, is encrypted and secure. Digital signature algorithms are used in the blockchain to establish proof of ownwership.

For Bitcoin they are used to prove that a sender of a transaction actually owns the coins he wants to transfer to another person. This security measure prevents people from unauthorised access to other people’s funds. To prove ownership a private key and a public key pair is generated by the user. When a user wants to prove ownership of some digital asset in a transaction, he or she has to sign the transaction with their private key.

Basically, what this means is the digital signature algorithm takes the transaction and the user’s private key as inputs and produces a digital signature as the output. The digital signature along with the transaction, is sent to the other nodes in the network. Since the user’s public key is available to anyone, any node can use the private key to decrypt the digital signature using a verification algorithm and check that a decrypted message matches the transaction message that was signed.

This proves ownership and that the transaction was not changed after it was signed. In this way, the users of the blockchain are guaranteed that only they are allowed to access their digital assets. It is very important for the user to keep their private key secure, otherwise anyone with their private key can steal their assets.

Now let’s take a look at cryptographic hash functions. You might already be familiar with has functions if you ever used the hash table data structure which is available in many popular programming languages. A regular hash function is basically a mathematical function that maps an input to an output. an example hash function would be the modular function.

Cryptographic hash functions are similar to regular hash functions, but with some added properties that make them more secure. However regular hash functions are usually faster in terms of performance so the trade off is speed versus security. Cryptographic hash functions have the following properties that make them secure: One, an input of any size will produce an output with a fixed size.

For example, if I pass the string, Blockgeeks is awesome into a hash function, I would get an output that looks like this.

1:) This prevents people from guessing the input based on the output

2:) The same input will always produce the same hash. And any small changes in the input will produce a completely different hash. This is useful for detecting any tampering with the input.

3:) It is computationally infeasible for someone to figure out the input based on the output hash. Computational infeasibility means that it would take an impractically long time, for example a billion years for someone to figure it out. Generally, two to the power of 80- computation steps is considered the lower bound for computational infeasability

4:) It is very difficult to find two inputs that would produce the same output. This is known as collision resistance. Note that cryptographic hash functions are different from encryption algorithms. Hash functions are a one-way function where an input is obfuscated, while encryption algorithms are a two-way function, where there is encryption and decryption. Encryption algorithms can be used to share a private message between parties securely and are also used for digital signatures.

Let’s take a look at how hash functions are used in blockchain.

The underlying data structure in the blockchain is essentially a linked list of blocks where each block contains some important information related to the use of the blockchain.

In the case of Bitcoin, it is a list of transactions that are being sent from one party to another. One property of the blockchain is that it’s append only. So, only new blocks can be added and old blocks cannot be changed. This immutability is achieved through the use of cryptographic hash functions. Essentially, each block in the blockchain. This block has is produced based on two things. The first is the block hash of the previous block in the blockchain and the other value is essentially the hash value of the information contained within that block. For example, transactions in the case of Bitcoin.

Therefore, if someone were to modify the contents of a block,by for example, changing one of the transactions in the block, then the hash of all the transactions would change and therefore the block hash itself would also change. Since the next block uses the block hash of the tampered block to calculate it’s block hash, that would change as well. This will produce a domino effect in which all the block hashes of all the blocks following the tampered block will change. Therefore, all other nodes in the network can detect a discrepancy to the main chain and woul reject the tampered block.

In this lesson we learned a little bit about the history of public key cryptography and hash functions. In our next lesson we’ll look at exactly how these things are used in blockchains to reach consensus.

Thanks for reading stay tuned for more lessons on the The New Next DB

This is the one they all do need to know.

Thx to Niloo Ravaei

A blockchain phenomena and screenwriter