Applying Symmetric Encryption
What AES is, and how to encrypt and decrypt a file with OpenSSL, read what the output tells you, and check that the file came back unchanged.
1AES
Computer encryption algorithms work on binary data. The most common symmetric algorithm is the Advanced Encryption Standard (AES). It secures Wi-Fi transmissions, web browsing, encrypted disks, and encryption built into processors.
AES is a symmetric block cipher: the same key encrypts and decrypts, and it encrypts data in 128-bit blocks (16 bytes). It can use keys of different lengths (128, 192, or 256 bits). Longer keys give more secure encryption but take more time to encrypt and decrypt.
2Tools for symmetric encryption
| Kind of tool | Example | Good for |
|---|---|---|
| Command line | OpenSSL | Scripts, servers, and precise control of every setting |
| Specialized software | AES Crypt (open source) | Encrypting a file with a right-click, no commands needed |
| Web-based tools | Many encryption websites | Quick practice; avoid them for real secrets, because the data passes through someone else's server |
3Encrypting and decrypting with OpenSSL
To encrypt a file named test with AES and a 128-bit key:
openssl enc -aes-128-cbc -e -in test -k password -out test.enc
To decrypt it with the same key:
openssl enc -aes-128-cbc -d -in test.enc -k password -out text
| Part | Meaning |
|---|---|
enc | Use OpenSSL's file encryption command |
-aes-128-cbc | AES with a 128-bit key, in CBC mode (each block is mixed with the one before it) |
-e / -d | Encrypt / decrypt |
-in / -out | The file to read / the file to write |
-k password | The password; the encryption key is derived from it |
Because it is symmetric, decryption needs the same algorithm, the same key length, and the same password used to encrypt.
4What happened to the file
Here is a real run of those commands on a 35-byte file:
$ cat test
Rehearsal moved to Room 214, 4 PM.
$ openssl enc -aes-128-cbc -e -in test -k password -out test.enc
*** WARNING : deprecated key derivation used.
Using -iter or -pbkdf2 would be better.
$ wc -c test test.enc
35 test
64 test.enc
99 total
$ head -c 8 test.enc
Salted__
$ openssl enc -aes-128-cbc -d -in test.enc -k password -out text
$ cmp test text && echo identical
identical
$ openssl enc -aes-128-cbc -d -in test.enc -k Password -out text2
bad decrypt
- The file grew from 35 to 64 bytes (Figure 1).
- The round trip worked:
cmpconfirms every byte oftextmatches the original. A command finishing is not proof; compare the files. - A wrong password fails: one capital letter is enough to get
bad decrypt. - The warning says the old password-to-key method is weak. Adding
-pbkdf2uses a slower, stronger method, which makes password guessing much harder.
The salt is random, so encrypting the same file with the same password twice gives different ciphertext. Two real runs with -pbkdf2 started 5A B0 A1 A8 0C DE 9C C6 and C9 BA F8 06 94 07 38 08. An attacker cannot tell that two encrypted files hold the same plaintext.
5Key length and speed
| Setting | Key length | Rounds of scrambling per block | Trade-off |
|---|---|---|---|
-aes-128-cbc | 128 bits | 10 | Fastest; already far beyond any brute-force search |
-aes-192-cbc | 192 bits | 12 | Middle |
-aes-256-cbc | 256 bits | 14 | Most secure; slowest |
More rounds mean more work per block, which is why longer keys encrypt and decrypt more slowly. A strong key does not rescue a weak password, though: if the key comes from password, an attacker guesses the password, not the key.
AES is a symmetric block cipher with 16-byte blocks and 128-, 192-, or 256-bit keys. With OpenSSL, -e encrypts and -d decrypts using the same cipher and password. Always check the decrypted file against the original.
6Worked examples
A 256-bit key
Write the OpenSSL command to encrypt grades.csv with AES using a 256-bit key and the password Tr0ut-Lake-91, saving the result as grades.enc.
- 1Cipher with a 256-bit key:
-aes-256-cbc. - 2Encrypt:
-e. Input and output:-in grades.csv -out grades.enc. - 3Password:
-k Tr0ut-Lake-91.
openssl enc -aes-256-cbc -e -in grades.csv -k Tr0ut-Lake-91 -out grades.encIt won't decrypt
A file was encrypted with -aes-256-cbc. A student runs openssl enc -aes-128-cbc -d -in grades.enc -k Tr0ut-Lake-91 -out grades.csv and gets bad decrypt. Why?
- 1Symmetric decryption must use the same algorithm settings as encryption.
- 2The file was encrypted with a 256-bit key; the command asks for 128 bits, so it derives a different key.
-aes-256-cbc -d.How big is the encrypted file?
Using the same command as above (no -pbkdf2), a 40-byte file is encrypted with AES. How big is the result?
- 140 bytes = two full 16-byte blocks (32) plus 8 bytes.
- 2The last 8 bytes are padded to a full block: 3 blocks = 48 bytes.
- 3OpenSSL adds its 16-byte header (Salted__ and the salt).
Phone or server?
A school wants to encrypt short daily attendance files on old tablets, and its decade-long archive of student records on a server. Which AES key lengths fit?
- 1Longer keys are more secure but slower.
- 2AES-128 is already far beyond brute-force reach and is lighter for old tablets.
- 3The archive is sensitive and long-lived, and the server has power to spare: AES-256.
-pbkdf2.7Common mistakes
-e encrypts and -d decrypts. Everything else in the two commands stays the same.
Symmetric means the same key. A different key length or password produces a different key and bad decrypt.
The key is derived from the password. AES-256 with -k password is only as strong as the word password.
Compare the decrypted file with the original, for example with cmp. Now and then a wrong password produces garbage with no error message at all, because the scrambled last block happens to look like valid padding.
“The command openssl enc -aes-128-cbc -e -in test -k password -out test.enc encrypts the file with AES, a symmetric block cipher, using a 128-bit key derived from the password. Running the same command with -d and the same password decrypts it, and comparing the result with the original confirms nothing changed.”
Name the algorithm, the key length, where the key comes from, and how you verified the result.