Skip to content

Lesson 2 of 3

Cryptography

Listen to an AI summary of Cryptography

0:00 / 0:38

Cryptography is the set of methods that keep data secret or show that data has changed. Code that picks the wrong method still runs with no error. A reviewer does not need the maths inside a method. A reviewer checks that the method fits what the data is for and that the method is current.

Three things the sports shop stores

For Sam, a customer, the sports shop stores a password, a home address and a cookie that says Sam is signed in.

The right protection depends on what the sports shop does with each piece of data later.

What is encoding

Encoding is changing the form of data so that the data can be sent or printed, and anyone can change the data back. Base64 is one encoding, which writes any data in letters, digits and a few symbols. The word hoodie in Base64 is aG9vZGll. Encoding uses no secret, so encoding protects nothing.

A hash for a password

The sports shop never reads Sam's password back, and only checks that a typed password matches. The shop saves a hash of the password. Which hash to use for a password is covered in a separate lesson.

Encryption for a home address

The sports shop prints Sam's home address on every parcel, so the sports shop must read the address back.

Encryption works like a locked box. The data is the thing inside the box, and the locked box is the encrypted data. The key to the box is the encryption key. Symmetric encryption is encryption where the same key locks and unlocks the data.

  1. hoodiethe word before any change
  2. aG9vZGllencoded in Base64. Anyone can get the word back
  3. a5ba5c1c84889214hashed. Nobody can get the word back
  4. 8e1f6b02c7d94a35encrypted. Only someone with the key can get the word back
The word hoodie encoded, hashed and encrypted, with the hash and the encrypted data cut short.

A message authentication code for a cookie

The cookie in Sam's browser says user=sam, and Sam can change the cookie to user=admin. The sports shop must spot the change. A hash beside the cookie does not help, since anyone can work out the hash of a new value.

A message authentication code, or MAC, is a hash made with a key. Only someone with the key can make or check a MAC. A MAC proves that a message is unchanged and that someone with the key made the message. A signed cookie is a cookie with a MAC added (Signed tokens).

Sam's password
Only checked, so the sports shop saves a hash.
Sam's home address
Read back for every parcel, so the sports shop saves the address encrypted.
Sam's cookie
Read back and must not change, so the sports shop adds a message authentication code.

An encrypted password is the wrong choice. Anyone who gets the key can read every password.

What is a nonce

With nothing else added, the same message encrypted twice with the same key gives the same result. Anyone who sees both results knows that the two messages match. A nonce is a value used once beside the key, so that the same message encrypts differently each time.

A nonce is not secret, and the code saves the nonce beside the encrypted data. The code makes each nonce and each key from a secure random number, a number that nobody can predict.

Methods that are out of date

A broken algorithm is a method of encryption or hashing that attackers have learned to defeat, such as the hash named MD5. Code that uses a broken algorithm has the weakness named use of a broken or risky cryptographic algorithm (CWE-327). A broken hash is use of a weak hash (CWE-328). MD5 and SHA-1 are a weakness where security rests on the hash, as with a password, a signature or a token. Both are fine for spotting accidental damage to a file, where no attacker chooses the data.

What is a cipher mode

A cipher mode is the way an encryption method, also called a cipher, is applied to a long piece of data. Some methods encrypt one small block of data at a time.

The mode named ECB encrypts every block on its own. Two blocks that hold the same data give the same encrypted block, so patterns in the data still show.

The mode named GCM hides the patterns and also detects any change to the encrypted data. With GCM, a nonce used twice with one key lets an attacker read parts of both messages and forge new ones.

The JavaScript below sets up encryption in two ways with crypto, a library that comes with Node. Look at the last three letters of the string in each version.

// Weakness: ECB mode, so patterns in the data still show
const cipher = crypto.createCipheriv('aes-256-ecb', key, null)

// Safer: GCM mode, with a new nonce for each message
const nonce = crypto.randomBytes(12)
const cipher = crypto.createCipheriv('aes-256-gcm', key, nonce)

createCipheriv sets up encryption. The string aes-256-ecb picks the method named AES and the mode ECB, and key is the key. In the safer version, crypto.randomBytes(12) makes a nonce from a secure random number, and the string aes-256-gcm picks the mode GCM.

Examples of what is current and what is out of date

Symmetric encryption
AES with the mode GCM is current. DES, RC4 and the mode ECB are out of date.
A hash
SHA-256 is current, and too fast for a password (Hashing and salts). MD5 and SHA-1 are broken where security rests on the hash.
A message authentication code
HMAC-SHA-256 is current.

Where the key is kept

A key typed into the code is a hard-coded secret (Secrets in code). Everyone who can read the code can read the key. A key saved in the same database as the encrypted data is no better. Someone who copies the database gets the key and the data together.

A key management service is a service that stores keys and uses the keys on behalf of an application. The application sends data to the service, and the service sends the encrypted data back. The key never leaves the service.

The JavaScript below shows a key in two places. Look at the string on the second line.

// Weakness: the key is typed into the code
const key = Buffer.from('0123456789abcdef0123456789abcdef')

// Safer: a key management service holds the key and encrypts the data
const result = await kms.encrypt({ KeyId: 'alias/address-key', Plaintext: homeAddress })

The second line sets key to a string typed into the code. In the safer version, kms.encrypt asks the key management service to encrypt the home address. KeyId names the key, and Plaintext is the data to encrypt.

Examples of what to look for

  • A password that is encrypted where a hash belongs, or a secret that is only encoded in Base64.
  • The names md5 or sha1 where the code hashes a password, a signature or a token. The names des or rc4 anywhere in new code.
  • The mode ECB.
  • A nonce typed into the code, or one nonce used for two messages.
  • A key typed into the code or the settings, or saved in the same database as the encrypted data.
  • An encryption method or a hash method written in the application's own code, where a library belongs.

Check your understanding

  1. State the difference between encoding and encryption.

    Show the answer

    Anyone can change encoded data back. Only someone with the key can read encrypted data.

  2. Which protection fits Sam's home address, a hash, encryption or a message authentication code?

    Show the answer

    Encryption, since the sports shop must read the address back for every parcel.

  3. Give one reason why a key is not saved in the same database as the encrypted data.

    Show the answer

    Someone who copies the database gets the key and the data together.

Practise it

Challenges with a weakness this lesson teaches.