(Very) Basic intro to AES-256 Cipher
Tags: cryptography, passwords, programming, security
Published: 2020-01-02 14:37:47

---
title: (Very) Basic intro to AES-256 Cipher
published: true
date: 2020-01-02 14:33:49 UTC
tags: Cryptography,Passwords,Programming,Security
canonical_url: https://qvault.io/2020/01/02/very-basic-intro-to-aes-256-cipher/?utm_source=rss&utm_medium=rss&utm_campaign=very-basic-intro-to-aes-256-cipher
---

![](https://qvault.io/wp-content/uploads/2020/01/1_Th9nPlIhYveMMsG9RvqQsQ-100x61.png)

AES stands for “Advanced Encryption Standard” and is a specification that has selected the Rijndael cipher as its symmetric key ciphering algorithm. Using AES, a message can be encrypted with a key (like a password) and no one except the key holder can decrypt the message. This is useful for many reasons, but a good example is a password manager that encrypts all of the user’s passwords using one master password. This is how [Qvault](https://qvault.io/), our free open-source password manager operates.

# Symmetric Encryption vs Asymmetric Encryption

![](https://qvault.io/wp-content/uploads/2020/01/1_Th9nPlIhYveMMsG9RvqQsQ-1024x621.png)<figcaption><a rel="noreferrer noopener" href="https://learn.g2.com/what-is-encryption" target="_blank">https://learn.g2.com/what-is-encryption</a></figcaption>

As shown above, ** symmetric encryption uses the same key**  for encryption and decryption and  **asymmetric encryption uses different keys.**

**Asymmetric**  encryption is preferred when you want someone to be able to send you encrypted data, but  **you don’t want to give them your private key**.

**Symmetric**  encryption is preferred when you are encrypting  **only for yourself**.

![](https://qvault.io/wp-content/uploads/2020/01/1_oe-Kk1FfLPNO5sAAl-4N-g.jpeg)<figcaption><a rel="noreferrer noopener" href="https://www.google.com/url?sa=i&amp;source=images&amp;cd=&amp;ved=2ahUKEwicx_jLitbiAhUNEHwKHeZtCPMQjRx6BAgBEAU&amp;url=https%3A%2F%2Fkullabs.com%2Fclasses%2Fsubjects%2Funits%2Flessons%2Fnotes%2Fnote-detail%2F7346&amp;psig=AOvVaw0z771qbnVkU95xmpfNxnyH&amp;ust=1559952561231880" target="_blank">Kullabs</a></figcaption>

# AES-256 Secret Key

The secret key used in AES-256 must be 256 bits long. In order to use a password or passphrase as the key, a [hashing algorithm](https://blog.goodaudience.com/very-basic-intro-to-hash-functions-sha-256-md-5-etc-ed721622ff8) can be used to extend the length.

The shorter the password or passphrase, the easier it is for an attacker to decrypt the data by guessing passwords, hashing them, and attempting to decrypt the message. In order to mitigate this threat, some applications enforce safeguards.

In the case of [Qvault](https://qvault.io/), the master password is hashed using the [Scrypt](https://en.wikipedia.org/wiki/Scrypt) algorithm in order to produce the private key. Scrypt is a very slow password-based key derivation function (similar properties to a hashing algorithm), which slows down attacks. Qvault also requires that passwords are at least 12 characters long, or encourages that users use a passphrase instead.

![](https://qvault.io/wp-content/uploads/2020/01/1_7v6djGHv-AC6Jeg9I5Eamg.png)<figcaption><a rel="noreferrer noopener" href="https://xkcd.com/936/" target="_blank">https://xkcd.com/936/</a></figcaption>

# Rijndael Encryption Process (Simplified)

1. Choose a password, then derive a short key from that password (using a function like [Scrypt](https://en.wikipedia.org/wiki/Scrypt) or [SHA-256](https://en.bitcoinwiki.org/wiki/SHA-256)). This short key will then be expanded using a [key schedule](https://en.wikipedia.org/wiki/Rijndael_key_schedule) to get separate “round keys” for each round of AES-256.

```
password: password12345678 →
short key: aafeeba6959ebeeb96519d5dcf0bcc069f81e4bb56c246d04872db92666e6d4b →
first round key: a567fb105ffd90cb
```

Deriving the round keys from the short key is out of the scope of this article. The important thing for us to understand is that a password is converted into round keys which are used in the AES ciphering process.

2. Choose a secret message:

```
Here is a secret
```

3. Encode the first round key and message in hexadecimal bytes and format them in 4×4 tables (top to bottom, left to right):

First Round Key:

```
61 66 35 39
35 62 66 30
36 31 66 63
37 30 64 62
```

Message:

```
48 20 61 63
65 69 20 72
72 73 73 65
65 20 65 74
```

4. Add the round key to the message (XOR). The corresponding cells in the message and key tables are added together. The output matrix will be used in the next step.

> 61 ⊕ 48 = 29
> 
> 35 ⊕ 65 = 50
> 
> …etc

```
29 46 54 5a
50 0b 46 42
44 42 15 06
52 10 01 16
```

5. In the resulting table, use the [substitution box](https://www.researchgate.net/figure/Rijndael-S-box-S-RD_fig7_325428613) to change each 2-character byte to its corresponding byte:

![](https://qvault.io/wp-content/uploads/2020/01/1_zLuZtQ6nFl0ADElv8lTSyg.png)<figcaption><a href="https://www.researchgate.net/figure/Rijndael-S-box-S-RD_fig7_325428613">https://www.researchgate.net/figure/Rijndael-S-box-S-RD_fig7_325428613</a></figcaption>

```
a5 5a 20 be
53 2b 5a 2c
1b 2c 59 6f
00 7c 7c 47
```

6. Shift rows. The first row doesn’t shift, the second-row shifts once, the third row twice, and the last row 3 times.

```
a5 5a 20 be
53 2b 5a 2c → 2b 5a 2c 53
1b 2c 59 6f → 2c 59 6f 1b → 59 6f 1b 2c
00 7c 7c 47 → 7c 7c 47 00 → 7c 47 00 7c → 47 00 7c 7c

a5 5a 20 be
2b 5a 2c 53
59 6f 1b 2c
47 00 7c 7c
```

7. Mix Columns. Each column is modulo multiplied by the [Rijndael’s Galois Field](https://www.samiam.org/galois.html). The math involved is outside the scope of this article, so I won’t be including the example output matrix.

![](https://qvault.io/wp-content/uploads/2020/01/1_RIsEo_r50T1595fhc7CKEw-1.png)<figcaption><a rel="noreferrer noopener" href="https://www.commonlounge.com/discussion/e32fdd267aaa4240a4464723bc74d0a5" target="_blank">https://www.commonlounge.com/discussion/e32fdd267aaa4240a4464723bc74d0a5</a></figcaption>

8. The output of the multiplication is used as the input “message” in the next round of AES. Each step is repeated 10 or more times in total, with one extra “add key” step at the end. Each round of “Add key” will use a new round key, but each new round key is still derived from the same password and short key.

- Add key
- Substitute bytes
- Shift rows
- Multiply columns

# That’s it! /s

Obviously the Rijndael cipher used in AES is fairly complex but I hope I’ve been able to shed light on a high-level view of what goes on inside! Thanks for reading.

Be sure to checkout [Qvault](https://qvault.io/), it’s an open-source password manager that uses AES-256 as the cipher.

# Sources

[https://www.tutorialspoint.com/cryptography/advanced\_encryption\_standard.htm](https://www.tutorialspoint.com/cryptography/advanced_encryption_standard.htm)

[https://crypto.stackexchange.com/questions/16125/does-the-secret-key-used-in-aes-have-to-prime](https://crypto.stackexchange.com/questions/16125/does-the-secret-key-used-in-aes-have-to-prime)

[https://en.wikipedia.org/wiki/Scrypt](https://en.wikipedia.org/wiki/Scrypt)

[https://www.samiam.org/galois.html](https://www.samiam.org/galois.html)

<iframe title="AES Rijndael Cipher explained as a Flash animation" width="742" height="557" src="https://www.youtube.com/embed/gP4PqVGudtg?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

## Thanks For Reading

Lane on Twitter: [@wagslane](https://twitter.com/wagslane)

Lane on Dev.to: [wagslane](https://dev.to/wagslane)

Download Qvault: [https://qvault.io](https://qvault.io/)

Star our Github: [https://github.com/q-vault/qvault](https://github.com/q-vault/qvault)

The post [(Very) Basic intro to AES-256 Cipher](https://qvault.io/2020/01/02/very-basic-intro-to-aes-256-cipher/) appeared first on [Qvault](https://qvault.io).