31

It obviously doesn't connect with any sort of database. How is this even possible?

Machavity
  • 3,766
  • 1
  • 14
  • 29
Andy_ye
  • 469
  • 4
  • 8

2 Answers2

71

Thinking of it as "password protection" slightly misrepresents the actual situation.

What happens when you password-protect a zip file is that the archive is encrypted using a symmetric algorithm (same key to encrypt and decrypt) using the password as the key.

The unzipper program "checks" whether the key is correct the same way I check whether the key to my front door is correct: If it opens the lock, it was the correct key.

So in this case the unzipper attempts to decrypt the data using the password you provide, and if the output is a properly structured archive, it was the correct password.

(I'm skipping the whole cryptography debate WRT collisions and possible duplicate keys for now; this is about how the concept works in theory rather than a specific implementation that may or may not have flaws)

EDIT: As user MobyDisk points out in comments, in the case of Zip specifically, the structure and the file tree are not encrypted, just the files themselves, as well as checksums for each file. If the password you use decrypts the file, and the decrypted checksum matches, you had the right password.

Shadur
  • 2,495
  • 21
  • 19
  • 30
    Unfortunately your house door key analogy doesn't work. Any decryption key will work in that you'll get an output, but only the correct one will produce your house when you open the door. All the incorrect keys will open the door but it won't be your house on the other side. – Eborbob Apr 21 '20 at 12:27
  • 78
    @eborbob Semantics. It's a metaphor, it doesn't have to map 1:1 to reality. – Shadur Apr 21 '20 at 12:28
  • 2
    ... Also, if it's possible to generate multiple keys so that decrypting the data encoded with one of them with any of the others will result in a properly structured archive with entirely different contents, you either did something terribly wrong with your algorithm,or terribly *right*. – Shadur Apr 21 '20 at 12:41
  • 32
    @Eborbob Any key will go into the lock and move the pins - only if the pattern is valid will the lock let you turn it. Any decryption key will go into the zip tool - only if the pattern is valid will the tool let you open it. Seems fine to me. – Chronocidal Apr 21 '20 at 14:57
  • 23
    I am now intrigued by the idea of a doorway that can take you to different places depending on what key you use to unlock it. – Seth R Apr 21 '20 at 16:19
  • 2
    @SethR see [Anywhere Key](https://lockekey.fandom.com/wiki/Anywhere_Key) – John Wu Apr 21 '20 at 20:41
  • @JohnWu I'm sure this has been done in fiction, but I don't think the Anywhere Key counts. For it to qualify, you would have to use a different key for each destination, not a single key that unlocks all destinations. – Brilliand Apr 22 '20 at 21:40
  • 12
    @SethR - That's an elevator in a building with full-floor apartment units. – bta Apr 22 '20 at 22:21
  • @Shadur It's not a metaphor, it is the explanation you gave, as follows: 'in the same way as'. The fact is that the unzip program checks, and doesn't decrypt if the password is wrong. – user207421 Apr 23 '20 at 08:27
  • 4
    @user207421 The unzip program "checks" by trying to decrypt, and determines that the password is wrong when it fails to decrypt. It's a metaphor, and it works just fine. – Shadur Apr 23 '20 at 08:28
  • 2
    @SethR Ever watched the 2nd Matrix movie? – Clockwork Apr 23 '20 at 10:12
  • 2
    @Clockwork, yeah, and the Keymaker did come to mind. But it's been so long since I saw it I couldn't quite a remember if that was his ability, or if he was just able to open any door. In any case, it's a fun premise for a story. – Seth R Apr 23 '20 at 13:46
36

It depends on the specific zip crypto algorithm.

For example, the original ZIP specification used the password to initialize a set of three 32-bit decryption keys. Then the ZIP header (12 random bytes placed at the beginning) was decrypted and then:

"After the header is decrypted, the last 1 or 2 bytes in Buffer SHOULD be the high-order word/byte of the CRC for the file being decrypted...This can be used to test if the password supplied is correct or not."

(ZIP Spec, section 6.1)

hft
  • 4,910
  • 17
  • 32
  • 3
    To summarize into the answer Andy is likely looking for: a "password protected" zip file is encrypted using the password as the key. If using the password with the decryption algorithm results in a proper zip file structure, the password was correct. – Shadur Apr 21 '20 at 12:02
  • @Shadur In the original ZIP file specification, the zip file structure itself is not encrypted. You can see the list of files even without the password. It looks like only the file and it's CRC are encrypted. – Moby Disk Apr 21 '20 at 13:44
  • @MobyDisk I see. Thanks for the clarification. – Shadur Apr 21 '20 at 13:51