The most popular use of blockchain is to create a cryptocurrency. As the word crypto is in cryptocurrency, you would expect that you need to master cryptography in order to become a blockchain programmer. That is not true. You only need to know two things about cryptography:
- Private key and public key (asymmetric cryptography)
- Hashing
These two have been explained in the previous part of this chapter. You don't need to know how to design a hashing algorithm or private key and public key algorithm. You only need to get an intuitive understanding of how they work and the implications of these technologies.
The implication of private keys and public keys is that it enables decentralized accounts. In a normal application, you have a username and password. These two fields enable someone to access their account. But having a private key and public key enables someone to have an account in a decentralized manner.
For hashing, it is a one-way function, meaning that given an input, you can get the output easily. But given an output, you couldn't get the input. A simple version of a one-way function would be this:

This is an addition process. If I tell you one of the outputs of this function is 999, and I ask you what the inputs are, you couldn't guess the answer. It could be anything from 1 and 998 to 500 and 499. A hashing function is something like that. The algorithm is clear as sky (you can read the algorithm of any hashing function on the internet), but it's hard to reverse the algorithm.
So, all you need to know about hashing is this: given input input you get this SHA-256 output (in hexadecimal): c96c6d5be8d08a12e7b5cdc1b207fa6b2430974c86803d8891675e76fd992c20. If you don't know the input, you couldn't get the input based on this output alone. Say you know the input input it is very prohibitive to find another input that produces the same output. We wouldn't even know whether such input exists or not.
That is all you need to know about cryptography when you become a blockchain developer. But that's only true if you become a certain type of blockchain developer, who creates a program on top of Ethereum.
Symmetric cryptography uses the same key between sender and receiver. This key is used to encrypt and decrypt a message. For example, you want to create an encryption function to encrypt text. Symmetric cryptography could be as simple as adding 5 to the text to be encrypted. If A (or 65 in ASCII) is the text to be encrypted, then this encryption function will add 5 to 65. The encrypted text would be F (or 71 in ASCII). To decrypt it, you just subtract 5 from the encrypted text, F.
Asymmetric cryptography is a different beast. There are two keys: a public key and a private key. They are linked with a special mathematical relationship. If you encrypt a message with a public key, you can only decrypt it with a private key. If you encrypt a message with a private key, you can only decrypt it with a public key. There is no straight relationship as with symmetric keys (adding and subtracting the same number) between a public key and a private key. There are a couple of asymmetric cryptography algorithms. I'll explain the easiest one, the RSA algorithm.
Generate two prime numbers, called p and q. They should be really big numbers (with at least hundreds of digits), but for this example, we choose low numbers: 11 and 17. These are your private key. Don't let someone know these numbers:
n = p x q
n is a composite number. In our case, n is 187.
Then, we find e number, which should be relatively prime, with (p-1)x(q-1):
(p-1) x (q-1) = 160
Relatively prime means e and (p-1) x (q-1) cannot be factorized with any number except 1. There is no number other than 1 that we can divide them by without a remainder. So, e is 7. But, e can be 11 as well. For this example, we choose 7 for e.
e and n are your public key. You can tell these numbers to strangers you meet on the bus, your grandma, your friendly neighbor, or your date.
Let's say the message we want to encrypt is A. In the real world, encrypting a short message like this is not safe. We have to pad the short message. So, A would be something like xxxxxxxxxxxxxxxxxxxA. If you check the previous script to encrypt a message earlier in this chapter, you would see there is a padding function. But for this example, we would not pad the message.
The encryption function is this:
encrypted_message = messagee (mod n)
So, the encrypted_message would be 65 ** 7 % 187 = 142.
Before we are able to decrypt the message, we need to find the d number:
e x d = 1 (mod (p-1) x (q-1))
d is 23.
The decryption function is this:
decrypted_message = encrypted_messaged mod n
So, the decrypted_message would be 142 ** 23 % 187 = 65. 65 in ASCII is A.
Apparently, xy mod n is easy to calculate, but finding the y root of integer module n is really hard. We call this trapdoor permutation. Factorization of n to find p and q is really hard (generating a private key from a public key). But, finding n from p and q is easy (generating a public key from a private key). These properties enable asymmetric cryptography.
Compared to symmetric cryptography, asymmetric cryptography enables people to communicate securely without needing to exchange keys first. You have two keys (private key and public key). You throw the public key out to anyone. All you need to do is to protect the secrecy of the private key. The private key is like a password to your Bitcoin/Ethereum account. Creating an account in any cryptocurrency is just generating a private key. Your address (or your username in cryptocurrency) is derived from the public key. The public key itself can be derived from the private key. An example of Bitcoin's private key in Wallet Import Format (WIF) is this: 5K1vbDP1nxvVYPqdKB5wCVpM3y99MzNqMJXWTiffp7sRWyC7SrG.
It has 51 hexadecimal characters. Each character can have 16 combinations. So, the amount of private keys is as follows: 16 ^ 51 = 25711008708143844408671393477458601640355247900524685364822016 (it's not exactly this amount, because the first number of a private key in Bitcoin is always 5 in mainnet, but you get the idea). That is a huge number. So, the probability of someone finding another account that is filled with Bitcoin already when generating a private key with a strong random process is very, very low. But the kind of account generated by a private key and public key does not have a reset password feature.
If someone sends Bitcoin to your address, and you forgot your private key, then it's gone for good. So, while your public key is recorded on the blockchain that is kept in every Bitcoin node, people are not going to get the private key.