Sub topic 8
Layer 6: Presentation, encoding, encryption, and compression
This chapter assumes you know the seven layers by name, how encapsulation wraps data, and what Layer 7 protocols such as HTTP do. You should also be comfortable reading short Python snippets.
What Layer 6 is responsible for
Layer 6 makes data usable on both ends of a connection. Two hosts may represent text, numbers, and structures differently. The presentation layer defines a shared format for the bytes that cross the wire. In the OSI model it has three jobs:
- Encoding: turning data into an agreed byte format, such as character sets and serialization formats.
- Encryption: making the bytes unreadable to anyone but the intended receiver.
- Compression: making the bytes smaller before they are sent.
The sender applies these steps on the way down. The receiver reverses them in the opposite order on the way up.
Encoding
A character encoding maps characters to bytes. ASCII covers 128 characters in one byte each. UTF-8 is the dominant encoding on the internet. It uses 1 to 4 bytes per character and is backward compatible with ASCII.
Encoding also covers byte order. A 32-bit integer can be stored most significant byte first (big-endian) or least significant byte first (little-endian). Network protocols use big-endian, called network byte order.
Serialization formats such as JSON, XML, Protocol Buffers, and ASN.1 also belong here. They turn in-memory structures into bytes that another machine can rebuild.
import gzip
text = "café ✓"
utf8 = text.encode("utf-8")
print(len(text), len(utf8)) # 6 9
# Decoding with the wrong character set produces mojibake
print("é".encode("utf-8").decode("latin-1")) # é
# Byte order: 1000 is 0x03e8
print((1000).to_bytes(4, "big").hex()) # 000003e8
print((1000).to_bytes(4, "little").hex()) # e8030000
# Compression: repetitive data shrinks, and decompression restores it exactly
payload = b"hello " * 100
packed = gzip.compress(payload)
print(len(payload), len(packed) < len(payload)) # 600 True
print(gzip.decompress(packed) == payload) # TrueThe Ethernet example in an earlier chapter used the same idea. Fields are read with "!6s6sH", where ! means network (big-endian) byte order.
Encryption
Encryption protects confidentiality and, with modern ciphers, integrity. TLS is the protocol most people think of. When you open https://example.com, TLS negotiates keys and then encrypts the HTTP bytes before TCP carries them.
TLS does not fit the model cleanly. It runs on top of TCP, so it is above Layer 4. It serves the application, so some people place it at Layer 6, some at Layer 5, and some at Layer 7. This ambiguity is normal. The model is a teaching tool, and real protocols often span layers.
Compression
Compression reduces the number of bytes sent. HTTP does this with the Accept-Encoding request header and the Content-Encoding response header. For example, a server may answer with Content-Encoding: gzip. Text such as HTML, CSS, and JSON compresses well. Already-compressed formats such as JPEG, MP4, and ZIP gain almost nothing.
Order matters when you combine steps. Compress first, then encrypt. Encrypted output looks random and cannot be compressed. Compressing secret data together with attacker-controlled data before encryption has also enabled attacks such as CRIME and BREACH, so TLS-level compression is disabled in modern TLS.
Where Layer 6 lives in practice
The TCP/IP model has no presentation layer. Its application layer covers Layers 5 to 7. In real software, the application or a library does the work: a JSON parser, a UTF-8 codec, a TLS library, or a gzip module. No separate "presentation protocol" sits between HTTP and TCP.
| Task | Typical example | Where it usually happens |
|---|---|---|
| Character encoding | UTF-8, ASCII | Application or runtime |
| Serialization | JSON, Protocol Buffers, ASN.1 | Application library |
| Encryption | TLS | TLS library, between application and TCP |
| Compression | gzip, Brotli | HTTP server and client |
Common errors
- Garbled text. The sender and receiver disagree on the character set. Check the
Content-Typeheader, for exampletext/html; charset=utf-8. - Wrong numbers from binary protocols. One side read the bytes in the wrong byte order.
- TLS handshake failures. The two sides share no cipher suite or protocol version, or the certificate is not trusted. The TCP connection itself succeeds, so
nc -zvpasses whilecurlfails. - Corrupt or unreadable bodies. A client that did not request compression receives gzip data, or a proxy decompresses a body without updating the headers.
Exercise
A client sends UTF-8 text, but the receiver prints "é" where "é" should appear. Which layer's job has gone wrong, and what is the likely cause?
Show answer
Layer 6, encoding. The receiver is decoding the UTF-8 bytes with a different character set, such as Latin-1. The fix is to make both sides agree on UTF-8, for example by declaring charset=utf-8 in the Content-Type header.
Exercise
A sender wants to both compress and encrypt a message. In which order should it do them, and why?
Show answer
Compress first, then encrypt. Encrypted data looks random and does not compress, so compressing afterward gains nothing.