One time pad

A one time pad takes plaintext and a pad (random garble) and XORs it together. Another person who has the same pad can then decipher this. A pad must never be re-used and must be discarded after deciphering.

Here is an example in C how a one time pad program might look like: int main(int argc, char *argv[]) { struct stat sb[2]; int fd[2]; u_char c, p; if (argc != 3) { fprintf(stderr, "usage: otp [plaintext] [pad]\n"); exit(1); } fd[PLAIN] = open(argv[1], O_RDONLY, 0); if (fd[PLAIN] < 0) { perror("open [PLAIN]"); exit(1); } if (fstat(fd[PLAIN], &sb[PLAIN]) < 0) { perror("fstat [PLAIN]"); exit(1); } fd[PAD] = open(argv[2], O_RDONLY, 0); if (fd[PAD] < 0) { perror("open [PAD]"); exit(1); } if (fstat(fd[PAD], &sb[PAD]) < 0) { perror("fstat [PAD]"); exit(1); } if (sb[PAD].st_size < sb[PLAIN].st_size) { fprintf(stderr, "pad must be larger or equal in size to plaintext\n"); exit(1); } while (read(fd[PLAIN], &c, sizeof(c)) > 0) { if (read(fd[PAD], &p, sizeof(p)) < 0) { perror("read [PAD]"); exit(1); } c ^= p;  write(STDOUT_FILENO, &c, 1); } return 0; }
 * 1) include 
 * 2) include 
 * 3) include 
 * 4) include 
 * 5) include 
 * 6) include 
 * 1) define PLAIN 0
 * 2) define PAD 1

And here is how it would be used:

atlas$ dd if=/dev/random of=pad bs=512 count=2 2+0 records in 2+0 records out 1024 bytes transferred in 0.000057 secs (17970574 bytes/sec) atlas$ ./otp otp.c pad | tee ciphertext | hexdump -C | head 00000000 47 41 5c ff 5a 58 8a bc  dd 69 90 88 be 3c 56 12  |GA\.ZX...i...#.s.SW<..^..| 00000050 3e 3b 44 2c c1 bc 42 10  1a 6f 04 90 30 e7 9f 26  |>;D,..B..o..0..&| 00000060 0a 12 47 e7 70 3e 1d 3e  9c 77 3c 9c 9f 50 31 49  |..G.p>.>.w<..P1I| 00000070 de d3 cf ed 51 8d 78 04  a0 70 8d d7 cc bd 2a f5  |....Q.x..p....*.| 00000080 7b 72 66 82 83 76 6b 71  6e f6 6a ba 6f 11 1c 57  |{rf..vkqn.j.o..W| 00000090  c6 41 25 ce bb 3f 80 9d  3e 82 fc bb 9f 4b d9 b7  |.A%..?..>....K..| atlas$ ./otp ciphertext pad | hexdump -C | head 00000000 23 69 6e 63 6c 75 64 65  20 3c 73 79 73 2f 74 79  |#include .#include | 00000020 3c 73 79 73 2f 73 74 61  74 2e 68 3e 0a 23 69 6e  |.#in| 00000030 63 6c 75 64 65 20 3c 66  63 6e 74 6c 2e 68 3e 0a  |clude .| 00000040 23 69 6e 63 6c 75 64 65  20 3c 75 6e 69 73 74 64  |#include .#include .#include | 00000070 3c 73 74 64 6c 69 62 2e  68 3e 0a 0a 23 64 65 66  |..#def| 00000080 69 6e 65 20 50 4c 41 49  4e 20 30 0a 23 64 65 66  |ine PLAIN 0.#def| 00000090 69 6e 65 20 50 41 44 20  31 0a 0a 0a 69 6e 74 0a  |ine PAD 1...int.|

A One Time Pad can be used by aircraft carriers who launch airplanes. The pad is made just seconds before launch in a secure part of the ship (data center) and uploaded through an umbilical cord to the airplane. Depending on how much of the pad is stored will securely encrypt communications between this carrier and the airplane. If the carrier sinks, the airplane has to switch to other secure communications to talk to other friendlies.