Glivate Glivate

Video · 7 min

How a torn QR code still scans

I tore the corner off this QR code. It still works. The card in the machine is a real QR code: SUNNY CAFE, version 1, at level H. It has 26 blocks: 9 hold the name and 17 are spares worked out from it, enough to fix 8 bad blocks.

Transcript

Click a sentence to play from there. Skip the transcript

The question0:00

I tore the corner off this QR code. It still works. My phone worked out what was missing. So I started wondering about the codes we pay with. On a shop counter, or a parking meter. Could someone change a few squares, and send your money to them instead? By the end you'll know why changing a code is harder than it looks. The easy trick scammers use instead. And how to catch it before you pay.

Nine blocks of name0:29

So where did the missing piece come from? Let's print a fresh code and look inside. This one says SUNNY CAFE, the name of a made-up shop. Look closely, and the squares sit in little clusters. Each cluster stores one small piece of the message. I'll call it a block. Now let's colour the blocks by what they hold. Only nine blocks hold the shop's name. The other seventeen are spare: extra blocks, worked out from those nine. How? I'll show you with a ruler in a moment.

The tear took the name1:01

Here's the torn card again, coloured the same way. The tear didn't only take spares. It took five of the nine name blocks. More than half the name. And the phone still read SUNNY CAFE. It rebuilt those blocks from the spares. So most of this code isn't the name at all. It's spare. And the spare can stand in for what's lost.

The ruler1:24

But a spare is only more squares. How could it know what the torn bit said? Here's the trick, with a ruler. Each block's piece of the message is stored as a number. So let's make each block a post, as tall as its number. Two posts are the message, and the ruler rests on them. Now I add two more posts, and raise each one until it touches the ruler. Those are the spares. They hold nothing new. They're worked out from the message, through the ruler.

Knock two down1:53

Now knock any two posts down. The ruler doesn't fall. It still rests on the other two, so it shows how tall the missing ones were. Push them back up to it, and they're mended. Our card works the same way, with a post for every block, name and spare. Engineers call this error correction. So two spares, and you can lose any two posts. The more spares, the more you can lose. That's how the torn card kept its name.

A black pen2:23

OK, that's tearing. Now back to your money. What if someone doesn't tear the code, but changes it? Here's a fresh card and a black pen. I colour in the white squares of one block. Nothing is missing now. The card looks perfect. On the ruler, that block's post isn't knocked down. It's pushed up, out of line. Looking at the card, you and I can't tell which block changed. But your phone can.

Line them up2:50

It holds the ruler up next to the tops, the way you'd check that a fence is straight. Three of them line up. One sticks up above the line: the one I changed. Finding it uses up one spare. Pushing it back down uses up another. And the screen still says SUNNY CAFE. So a small change isn't an edit. To your phone, it's damage, and it gets fixed.

Keep colouring3:16

So one changed block gets fixed. Fine. But a scammer wouldn't stop at one, would they? Let's keep colouring, one block after another. This gauge counts the changed blocks. Each one uses up two spares, so this card can fix eight. Up to this line, the screen still says SUNNY CAFE. One more, and it says nothing at all. Not a wrong name. The phone only fixes what it can be sure of. Past the line, it can't be sure, so it won't guess. The pen didn't change the code. It broke it.

A name you choose3:50

So could a careful scammer do it properly? Say they want this card to read QUICK PAY, their own name. They rewrite the name blocks. One already matches, so that's eight changes, right at the line. The phone fixes them all, and SUNNY CAFE comes back. Next, they redraw the spares too. Now it reads nothing, until two thirds of the card is redrawn, exactly right. Then enough spares agree with the new name, and it says QUICK PAY. So a few changes get put back. More breaks it. A new name takes a big, exact redraw.

A sticker4:27

Then how do people really lose money to QR codes? They don't touch the code at all. Here's our shop's code on the counter. Someone prints a whole new code and sticks it on top. The phone reads it first time. QUICK PAY. There's nothing to repair. It's a perfect code. Look inside. The shop's code is still underneath, untouched. The sticker is a separate code.

It's real4:51

This isn't a made-up worry. In Austin, Texas, someone stuck fake codes on twenty-nine city parking pay stations. And the FTC, the US government agency that protects shoppers, has warned about the same trick. Nothing in a QR code says who made it. It only checks itself for damage. So scammers don't edit your code. They cover it. And the new one is a perfect code.

Back to the counter5:17

So let's go back to the counter. Can someone change the code you pay with? Changing it is hard, and it usually breaks it. Covering it is easy. A QR code checks for damage, never for who made it. So before you pay, look for a sticker on top, and read the name or the link your phone shows. If it doesn't match the shop, or the person behind the counter, ask.

Build it5:41

One last thing, a bonus for anyone who codes. If you don't, you've already got the whole story. Here it is in Python. The first line brings in a ruler someone else built: a Reed–Solomon code. Seventeen spares, like our card. Encode SUNNY CAFE, which means turn it into a code: that's the printer. Change one block's number, like our pen did. Then decode it, which means read it back. The block comes back. Keep changing blocks, past the gauge's line, and it gives up. No code. Then encode QUICK PAY from scratch. It reads perfectly.

The code

repair.py Python
from reedsolo import RSCodecrs = RSCodec(17)  # seventeen sparescard = rs.encode(b"SUNNY CAFE")card[0] ^= 15  # the penprint(rs.decode(card)[0].decode())for i in range(1, 9): card[i] ^= 15try: rs.decode(card)  # the gaugeexcept Exception: print("no code")sticker = rs.encode(b"QUICK PAY")print(rs.decode(sticker)[0].decode())
Output
SUNNY CAFE
no code
QUICK PAY