Bits, bytes and text

Unit 1 of 5 in How computers and the internet work.

You will be able to explain how text is stored as bytes and why decoding something is not the same as breaking protection.

Lesson

Everything is a number

A computer stores everything as numbers, written in bits (a 1 or a 0). Eight bits make a byte. Internet standards usually say octet for a group of eight bits.

Text is no different. Each character, such as the letter A or the pound sign, has a number assigned to it in a big shared table, the Universal Character Set (ISO/IEC 10646), which covers most of the world's writing systems. A character encoding is the rule for turning those numbers into bytes and back.

UTF-8: one encoding for most of the world

UTF-8 is the encoding most of the web uses. It writes each character using between one and four bytes. A character that fits in the first 128 slots, the old US-ASCII range, uses a single byte whose top bit is 0, leaving seven bits for the number.

That choice was deliberate. The standard says UTF-8 keeps the full US-ASCII range so that file systems, parsers and other software that depend on ASCII values keep working.

Why a security person cares about decoding

UTF-8 has rules about which byte patterns are legal. Software that decodes it must reject the illegal ones. The standard warns that an attacker could exploit a careless parser by sending a byte sequence the rules forbid.

Its own example: a program might refuse the NUL character when it is written as the single byte 00, but wrongly accept the illegal two-byte form C0 80 and treat it as NUL. The check and the real decoding disagreed. The defensive lesson is general: validate input in the same form you will use it, and reject what is not valid.

Encoding is not secrecy

Base64 is another encoding. It turns any bytes into a limited set of printable characters. The standard says it exists for places that are limited to US-ASCII text, and that it also makes it possible to handle objects in text editors.

Base64 has a public, fixed alphabet and a fixed procedure. There is no key anywhere in it, so anyone can reverse it. If you ever see a password or token that is only Base64, it is readable by whoever finds it. Hiding information needs encryption, which you will meet in the cryptography class.

Key points

Sources

Practice in the labs

Decode a few things by hand so the idea sticks: you will see how little protection an encoding gives.

Knowledge check

  1. A password in a config file is stored as Base64. How protected is it? Answer: Not protected: anyone can decode Base64 without a key. Base64 has a public alphabet and no key (RFC 4648), so it is only a different way of writing the same bytes. Anyone can reverse it.
  2. What is the main reason UTF-8 keeps the ASCII range as single bytes? Answer: So software that relies on ASCII values keeps working. The standard says UTF-8 preserves the full US-ASCII range for compatibility with file systems, parsers and other software that depend on those values.
  3. A filter blocks the single byte 00 but a decoder later accepts C0 80 as NUL. What went wrong? Answer: The filter checked a different form than the decoder used. RFC 3629 gives exactly this example: the validity check and the decoding disagreed about what the bytes meant. Validate the form you will actually use and reject illegal sequences.

Back to the syllabus or see all classes.

Related