Crypto security basics: Staying safe in Web3

In previous articles, we’ve learned how to use Web3, smart contracts, and decentralized applications to build secure applications. 

In this article, we’ll cover some of the cryptographic basics you’ll need in order to safely interact with Web3 and networks like Ethereum.

Cryptography

Cryptography is the study and science of cryptography. It’s the study and science of how to represent information in such a way that it can’t be read without the right decryption keys. 

The information encoded in a way that can be read by someone else is encrypted and information encoded in a way that can’t be read by anyone else is unencrypted. 


Cryptography is therefore a form of digital signature and a form of data encryption.

Cryptography is often used in the design of financial institutions, websites, web services, and even in a wide variety of other fields. 

It’s used to secure user data, protect the integrity of transactions, and generally protect the information being transmitted from one party to another.

We’ll be using cryptography for two purposes in Web3. 

Firstly, we’ll be looking at how to encrypt data, preventing people from viewing it if they have access to the decryption keys. 

Secondly, we’ll be looking at how to verify signatures, ensuring that messages sent on a network are being sent by the intended sender.

For both of these purposes, we’ll be using symmetric encryption algorithms. 

These algorithms encrypt and decrypt information using the same secret key.

Symmetric encryption is one of the oldest forms of cryptography and we’ll use it here to show how it works. 

In the next section, we’ll learn a few ways that public keys can be used with asymmetric encryption, allowing more complex types of cryptography to be used in the future.

Symmetric cryptography

In symmetric cryptography, the same key is used to encrypt and decrypt information. 

Information is encrypted with the key using a secret algorithm.

The symmetric algorithms in Web3 are AES and ChaCha20. 

For both, you’ll need the same secret key, to encrypt or decrypt information.

The same algorithm is used both to encrypt and decrypt the information. 

It’s impossible to tell from just the algorithm whether the information is being encrypted or decrypted.

Encrypting data

Encrypting data in Web3 using AES is easy. 

You’ll need a key and an input.

Here, we’ve encrypted the string “This is encrypted data”.


To decrypt data, you’ll need to provide the same secret key and you’ll have to provide the same input to the algorithm as when encrypting it. 

The input can either be the same string or a more complex structure containing more data.

As you can see, you’ll need the same secret key for both encrypting and decrypting data, and the same input, whether that be a string, an integer, or whatever else.

Encrypting multiple pieces of data

Encrypting data in Bhero.com Web3 using AES is easy. 

You’ll need a key and an input.

Here, we’ve encrypted the strings “This is encrypted data”, “This is another encrypted string”, and “And another encrypted string”.

To decrypt data, you’ll need the same secret key and you’ll have to provide the same input to the algorithm as when encrypting it.

As you can see, you’ll need the same secret key for all of the data being encrypted.

As an alternative, you could use multiple inputs. 

This is known as key-based asymmetric encryption.

Decrypting data

In the same way that encryption uses a secret key, so does decryption.

In this case, the key is “This is encrypted data” and the input is “And another encrypted string”.

Note that, unlike encryption, you do not have to provide a secret key to decrypt the data.

Decrypting data in Web3 using ChaCha20 is easy.

You’ll need a key and an input.

Here, we’ve encrypted the string “This is encrypted data” with the key “This is encrypted data”.

We’ve also encrypted the string “This is another encrypted string” with the key “This is encrypted data”.

To decrypt data, you’ll need the same secret key and you’ll have to provide the same input. 

We hope you enjoyed reading, that's it for today, see you in the next articles.

Comments