Skip to main content
Back to projects

LFSR Image Encryption

Cryptography

Fundamentals Interactive
Python LFSR
Interactive demo XOR encryption with a shift register

Upload a small image. A linear-feedback shift register (LFSR) generates a pseudo-random keystream, and each pixel byte is XOR-ed with it. Because XOR is symmetric, XOR-ing the encrypted bytes with the same keystream restores the original exactly.

Original

Encrypted

Decrypted

Upload an image to see the round-trip. The encrypted pane should look like noise.

How it works

  1. 1 Load an image
  2. 2 Generate an LFSR keystream
  3. 3 XOR each byte
  4. 4 XOR again to decrypt

How it works

A linear-feedback shift register (LFSR) produces a long, pseudo-random bit sequence from a small seed. Each pixel byte is XOR-ed with the keystream to scramble it, and XOR-ing again with the same keystream restores the original — encryption and decryption are the same operation.

Building it, step by step

1. Read bytes

The image is converted to grayscale so each pixel is a single byte from 0–255.

2. Step the register

The LFSR shifts its state and feeds a few tapped bits back in, producing the next keystream byte.

3. XOR to encrypt and decrypt

The plaintext and keystream are XOR-ed. Repeating the XOR with the same seed undoes it exactly.

What I learned

An LFSR is a stream cipher, not a modern secure cipher — its output is statistically uniform but linear, so the sequence is predictable from a short run of output. It is a great way to see how a keystream works, and why real systems use much stronger constructions.