Noise with a fingerprint

Compression vs Encryption: Visual Recognition of Random-Looking Data

A firmware image often contains a block that looks like pure noise. Whether it is compressed or encrypted decides the next hour of work, and an entropy plot alone cannot tell the two apart.

Hexfield editorial

6.32 bit/bytePlain .text entropy
6.66 bit/byte, ratio 0.842LZ-style output
8.00 bit/byteDEFLATE level 9
8.00 bit/byteSHA-256-CTR stream
Byte histogram of a gzip stream: 256 bars of nearly equal height with small but visible deviations
Byte histogram of a gzip stream: nearly flat, yet uneven enough to fail a chi-square test that a stream cipher passes.

Compression vs encryption is the first question about any blob that sits near 8 bits per byte. Both kinds of output look like noise in a hex view, and both push a sliding entropy plot to the top of its scale. Telling compression and encryption apart still matters a great deal: compressed data can be unpacked in seconds once the algorithm is known, while encrypted data needs a key, which usually means going back to the code that reads it. This guide uses real measurements on one input, 400 KB taken from the .text section of the GNU Bash 5.2 ELF, to show which tests separate the two and which do not.

Compression vs encryption: why entropy fails

Plain machine code from that .text region measures 6.32 bits per byte. An LZ-style compressor without an entropy stage brought it to a ratio of 0.842 and raised the entropy to 6.66. DEFLATE at level 9 produced output at 8.00, and a SHA-256 counter-mode stream cipher over the same input also produced 8.00. Two completely different transformations, one reversible by anyone and one reversible only with a key, end up at the same number to two decimal places.

Shannon entropy over a window counts only how evenly the 256 byte values are used. A good entropy coder such as Huffman, a range coder or FSE is designed to use all codes almost evenly, so it reaches the same ceiling as a cipher. Entropy is useful for finding the region; it cannot classify it. Every reliable method for separating compression and encryption therefore looks at something else: the header, the shape of the histogram, or the local structure that only a picture shows.

Headers and magic bytes

The cheapest test is still the best one. Most formats announce themselves in the first bytes, and many embedded blobs keep their headers even when the surrounding container is custom.

Format Leading bytes (hex) Notes
gzip 1F 8B 08 08 is the DEFLATE method
zlib 78 01 / 78 5E / 78 9C / 78 DA second byte depends on level
bzip2 42 5A 68 (“BZh”) + level digit blocks begin 31 41 59 26 53 59
xz FD 37 7A 58 5A 00 LZMA2 inside
zstd frame 28 B5 2F FD
LZ4 frame 04 22 4D 18
Snappy framed FF 06 00 00 73 4E 61 50 70 59 “sNaPpY” identifier
OpenSSL enc 53 61 6C 74 65 64 5F 5F (“Salted__”) 8-byte salt follows

binwalk scans for these signatures at every offset. A raw stream without a header is harder, but DEFLATE can still be tested directly: try zlib.decompressobj(-15) from every candidate offset and keep the ones that decode more than a few hundred bytes. Ciphertext also has structural hints: a length that is a multiple of 16, a 16-byte IV or an 8-byte salt in front, an HMAC tag of 32 bytes at the end.

Byte histogram and chi-square tests

A byte histogram of 256 bins is flat for both a cipher and a strong compressor, but not equally flat. Pearson’s chi-square statistic measures how far the counts deviate from the expected n/256 per bin. For truly uniform bytes it follows a chi-square distribution with 255 degrees of freedom: mean 255, standard deviation about 22.6, and values above roughly 310 happen less than 1% of the time.

On the same 400 KB input the measured values were 255.9 for the SHA-256-CTR stream, right on the expected mean, and 961.4 for DEFLATE at level 9, about 31 standard deviations too high. bzip2 gave 871.0. The tool ent reports the same statistic as a percentage; a result below 1% or above 99% means the data is not uniformly random. That is the most reliable quick separation for compression vs encryption, with one exception that the next section covers.

Visual recognition of compressors

Compressors differ in what they leave in the stream, and that is what makes visual recognition possible. Recognition works best on slices of several megabytes; on a few kilobytes many artifacts never appear.

LZ4 and Snappy are byte-oriented LZ77 variants with no entropy coding. LZ4 writes a token byte whose high nibble is the literal length and low nibble the match length, then raw literals, then a 2-byte little-endian offset. Snappy uses a tag byte whose two low bits give the element type. Literals pass through unchanged, so strings still finds fragments of text in the output and the entropy stays well below 8; the 6.66 bits per byte measured above is typical. In a trigram view the literal regions look like the original data with gaps.

DEFLATE, LZMA and zstd all add an entropy stage after LZ77 matching. DEFLATE uses Huffman codes with a 32 KB window and restarts its code tables at every block, which leaves faint planes and stripes in a rotated trigram cloud and the chi-square excess measured above. zstd combines Huffman-coded literals with FSE for the sequences, and its 4-byte frame magic is usually present. LZMA uses a context-modeled range coder, and its output is the hard case: on the test input it scored a chi-square of 245.1 at 7.999 bits per byte, inside the band a cipher produces. Comparing DEFLATE vs LZMA visually gives the same answer, since DEFLATE output shows structure on a rotated cube and LZMA output does not. bzip2, with its Burrows-Wheeler and move-to-front stages, tends to show a bright point at (0xFF, 0xFF, 0xFF).

The trigram view explained step by step shows how to read these clouds, and the layered digram section helps with locating headers inside a long stream.

Recognition checklist for a blob

  1. Search for magic bytes at offset 0 and throughout, with binwalk or a short Python loop.
  2. Run a sliding entropy plot to find region boundaries; a step from 6 to 8 marks where compressed or encrypted data begins.
  3. Compute the chi-square of each region. A result near 255 is compatible with encryption or LZMA; a result far above 310 points to compression or weak encryption.
  4. Try raw DEFLATE and LZMA decoders at the region start.
  5. Rotate a trigram view of a slice from the middle of the region, away from any header.
  6. Check the length modulo 16 and look for repeating 16-byte blocks, which would indicate AES in ECB mode.

Weak encryption and its traces

XOR with a short repeating key and AES in ECB mode are the two schemes most often met in firmware and CTF challenges, and neither survives a picture. XOR keeps the plaintext statistics, so entropy and chi-square both stay far from random, and the key length shows up as a period. ECB repeats identical ciphertext blocks wherever the plaintext repeats. The Veles tool page describes one viewer that shows both effects in a trigram cloud. The deflate vs lzma comparison above is the main limit of the method: when the statistics and the picture both look random and no header exists, only the code that consumes the blob can settle compression vs encryption.

Questions readers ask

Is a chi-square test enough to prove data is encrypted?

No. It shows only that byte frequencies are consistent with a uniform source. LZMA output passed in the measurement on this page, and a well-designed compressor with a strong entropy coder can do the same. Treat a pass as compatible with encryption, then look for headers, padding to the cipher block size, key-derivation salts and the code that reads the blob.

What does XOR with a short key look like?

Nothing like real encryption. XOR with a repeating key keeps the statistics of the plaintext, only shuffled, so entropy stays near the plaintext level and the trigram cloud keeps structure. A key of length k makes the data periodic with period k, which an autocorrelation or a layered view exposes. Zero runs in the plaintext reveal the key bytes directly.

Why does AES in ECB mode look different from CBC or CTR?

ECB encrypts each 16-byte block independently with the same key, so identical plaintext blocks give identical ciphertext blocks. Padding, zero runs and repeated records turn into repeated 16-byte patterns that a layered digram or a simple duplicate-block count finds at once. CBC and CTR mix in an IV or counter, so repeats disappear.

Where can I see the trigram patterns of each compressor?

Compress a known file with several algorithms, cut a region from the middle of each stream to avoid headers, and load the pieces side by side in a trigram viewer such as Cantor Dust or the Veles 2018.05 build. The patterns are subtle on small inputs, so use several megabytes per sample.

New to capture the flag? Formats, teams and a first-month plan in one place.

Read the CTF guide