top of page

Compression and Encryption - How does it look in binary?

  • Writer: prabhu perumal
    prabhu perumal
  • Sep 6, 2025
  • 3 min read

When managing files on a computer, you might encounter the terms "encryption" and "compression." Both alter your data, but for entirely different purposes. Let's explore what each does, how they appear in binary, and which should be done first. 


Encryption is for security.


The purpose of encryption is to render data unreadable to anyone without a secret key. It transforms your information into a secret code, often called "ciphertext." If someone intercepts an encrypted file without the key, they will only see a meaningless jumble of data.


  • Purpose: To protect information and maintain confidentiality.

  • How it works: Utilizes a mathematical algorithm and a "key" to scramble data.

  • Effect on size: The file size typically remains the same or becomes slightly larger.


It's like locking a diary. Only the person with the correct key can unlock it and read the contents.


What is Compression?


Compression is for efficiency.

The aim of compression is to reduce file size. This conserves storage space and speeds up file transfer over the internet. It operates by identifying and removing repetitive information in a file.


  • Purpose: To decrease file size.

  • How it works: Identifies patterns in the data and stores them more efficiently.

  • Effect on size: The file size is reduced.


It's like vacuum-sealing clothes for a suitcase. You're not altering the clothes, just packing them into a smaller space. They can be restored to their original form later.


A Binary Example: The Word "HELLO"


Computers store all data as a series of 1s and 0s, known as binary. Let's examine how encryption and compression affect the binary code for the word "HELLO."

First, here is "HELLO" in standard ASCII binary:


Original "HELLO": 01001000 01000101 01001100 01001100 01001111
1. Encrypting "HELLO"

We'll use a simple encryption method that shifts each letter forward by 3 places (H -> K, E -> H, etc.). The encrypted word is "KHOOR".

Encrypted "KHOOR" in Binary: 01001011 01001000 01001111 01001111 01010010

Result: The binary code is completely different, making the original word unreadable. The length of the data (5 bytes) has not changed.


2. Compressing "HELLO"


A simple compression method called Run-Length Encoding (RLE) looks for repeating characters. In "HELLO," the letter 'L' appears twice in a row. The RLE method can represent "LL" as "2L". The compressed result is "HE2LO".

Compressed "HE2LO" in Binary: 01001000 01000101 00110010 01001100 01001111

Result: The algorithm replaced a pattern to try to save space. While this simple example didn't make the file smaller, it shows the technique. For a file with a lot of repetition, like "AAAAAAAAAA", compression would make it much smaller ("10A").


Which Comes First: Compressing or Encrypting?


If you need to both compress and encrypt a file, the correct order is very important.

The Rule: Compress first, then encrypt.


  1. Compress First: Compression works best on data with predictable patterns. A text document or a spreadsheet has many patterns that a compression algorithm can find and shrink.

  2. Then, Encrypt: After the file is smaller, you can encrypt it. Good encryption turns the data into a random-looking string of 1s and 0s with no patterns.


What happens if you do it wrong? (Encrypt first, then compress)


If you encrypt a file first, you create a block of random-looking data. A compression tool can't find any patterns in random data, so it won't be able to make the file smaller. In fact, it might even make the file slightly larger by adding its own header information.


What About Password-Protected ZIP Files?

Tools like ZIP, 7z, or RAR that let you add a password handle both processes for you. When you create a password-protected archive, the software automatically follows the correct order: it compresses your files first to make them smaller, and then it encrypts the resulting archive with a key generated from your password.

Comments


bottom of page