liveBook · Manning

One-time pad

As explained in Classic Computer Science Problems in Swift: Essential techniques for practicing programmers

A symmetric encryption technique that combines plaintext with an equally long, secret, random pad so that the plaintext cannot be recovered without both resulting components.

A one-time pad encrypts data by combining each plaintext byte with equally long random dummy data. Encryption produces two components: the pad and the encrypted product. Either component alone is useless; recombining both recovers the plaintext. section 1.3

For the technique to provide its intended protection, the dummy data must be:

  • As long as the original data, preventing repeated patterns caused by a short pad. section 1.3
  • Random and completely secret; reused or partially revealed pad data gives an attacker a clue. section 1.3

Swift byte-oriented implementation

Represent keys as [UInt8], generate a pad with the plaintext's UTF-8 byte count, and XOR corresponding bytes. Since A ^ B = C and C ^ B = A, decrypting XORs the two resulting keys again and converts the bytes back to a String. The demonstrated generator uses arc4random_uniform(), which is close to random for the example but not truly random. section 1.3

Original Data + Dummy Data -Encryption> Key-Pair (Dummy Data, Product)
 -Decryption> Original Data

Follow the encryption and decryption flow from original data to separated key pair and back again. section 1.3

One-time pads are impractical for general encryption because both parties must possess the same secret pad. A practical variation is to implement the technique for encrypting and decrypting images. section 1.6 section 1.7

How Classic Computer Science Problems in Swift: Essential techniques for practicing programmers covers it

How deep it goes
Explains pad criteria, UTF-8 byte handling, XOR, and Swift encryption and decryption, but stops short of formal security proofs.
Examples
Demonstrates encrypting a UTF-8 String in Swift and proposes encrypting and decrypting images.
What it assumes
Assumes basic Swift and byte familiarity while teaching UTF-8 views, XOR reversal, and key-pair handling.
What you can do after
Build it from scratch with Swift byte arrays, random pads, XOR encryption, and UTF-8 decryption.
How it connects
Connects symmetric encryption with UTF-8 encoding, XOR, random number generation, and key management.

Also explained in

Compare how 2 books explain one-time pad →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Classic Computer Science Problems in Swift: Essential techniques for practicing programmers. The section links point to the parts of the book it draws on.