AP Cybersecurity · Study guide

Applying Symmetric Encryption

AP® Cybersecurity · Unit 5 · Applications, Data & Cryptography

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 toolExampleGood for
Command lineOpenSSLScripts, servers, and precise control of every setting
Specialized softwareAES Crypt (open source)Encrypting a file with a right-click, no commands needed
Web-based toolsMany encryption websitesQuick 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:

Encrypt
openssl enc -aes-128-cbc -e -in test -k password -out test.enc

To decrypt it with the same key:

Decrypt
openssl enc -aes-128-cbc -d -in test.enc -k password -out text
PartMeaning
encUse OpenSSL's file encryption command
-aes-128-cbcAES with a 128-bit key, in CBC mode (each block is mixed with the one before it)
-e / -dEncrypt / decrypt
-in / -outThe file to read / the file to write
-k passwordThe 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:

Terminal session
$ 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: cmp confirms every byte of text matches 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 -pbkdf2 uses a slower, stronger method, which makes password guessing much harder.
Figure 1. AES always works on 16-byte blocks, so the last block is padded. OpenSSL stores the random salt at the front of the file so decryption can rebuild the same key.

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

SettingKey lengthRounds of scrambling per blockTrade-off
-aes-128-cbc128 bits10Fastest; already far beyond any brute-force search
-aes-192-cbc192 bits12Middle
-aes-256-cbc256 bits14Most 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.

The key idea

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

Worked Example 1 Write the command

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.

  1. 1
    Cipher with a 256-bit key: -aes-256-cbc.
  2. 2
    Encrypt: -e. Input and output: -in grades.csv -out grades.enc.
  3. 3
    Password: -k Tr0ut-Lake-91.
Answer: openssl enc -aes-256-cbc -e -in grades.csv -k Tr0ut-Lake-91 -out grades.enc
Worked Example 2 Find the error

It 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?

  1. 1
    Symmetric decryption must use the same algorithm settings as encryption.
  2. 2
    The file was encrypted with a 256-bit key; the command asks for 128 bits, so it derives a different key.
Answer: The key length does not match. Use -aes-256-cbc -d.
Worked Example 3 Predict the size

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?

  1. 1
    40 bytes = two full 16-byte blocks (32) plus 8 bytes.
  2. 2
    The last 8 bytes are padded to a full block: 3 blocks = 48 bytes.
  3. 3
    OpenSSL adds its 16-byte header (Salted__ and the salt).
Answer: 16 + 48 = 64 bytes.
Worked Example 4 Choose the key length

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?

  1. 1
    Longer keys are more secure but slower.
  2. 2
    AES-128 is already far beyond brute-force reach and is lighter for old tablets.
  3. 3
    The archive is sensitive and long-lived, and the server has power to spare: AES-256.
Answer: AES-128 on the tablets; AES-256 for the archive. Either way, use a strong password and -pbkdf2.

7Common mistakes

Common Mistake 1 · Mixing up -e and -d

-e encrypts and -d decrypts. Everything else in the two commands stays the same.

Common Mistake 2 · Changing the cipher between encrypt and decrypt

Symmetric means the same key. A different key length or password produces a different key and bad decrypt.

Common Mistake 3 · Trusting a weak password with a strong cipher

The key is derived from the password. AES-256 with -k password is only as strong as the word password.

Common Mistake 4 · Assuming success without checking

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.

Explain a symmetric encryption step like this

“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.