One time pad: Difference between revisions

From Hackepedia
Jump to navigationJump to search
Created page with "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. Here is an example in C how a one..."
 
example of an otp
Line 2: Line 2:


Here is an example in C how a one time pad program might look like:
Here is an example in C how a one time pad program might look like:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#define PLAIN 0
#define PAD 1
 
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;
}
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...<V.|
00000010  27 bd 65 18 37 1a c2 86  c8 d5 c6 31 ef ad 4d c2  |'.e.7......1..M.|
00000020  30 91 68 c0 ab 1a 74 77  5a 90 66 7f 4f 11 ea 05  |0.h...twZ.f.O...|
00000030  d4 99 24 d9 15 86 59 64  e0 6f 7c 9d 4a 36 ee 2a  |..$...Yd.o|.J6.*|
00000040  04 52 38 3e 23 b1 73 e8  53 57 3c 14 11 5e ba df  |.R8>#.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 <sys/ty|
00000010  70 65 73 2e 68 3e 0a 23  69 6e 63 6c 75 64 65 20  |pes.h>.#include |
00000020  3c 73 79 73 2f 73 74 61  74 2e 68 3e 0a 23 69 6e  |<sys/stat.h>.#in|
00000030  63 6c 75 64 65 20 3c 66  63 6e 74 6c 2e 68 3e 0a  |clude <fcntl.h>.|
00000040  23 69 6e 63 6c 75 64 65  20 3c 75 6e 69 73 74 64  |#include <unistd|
00000050  2e 68 3e 0a 23 69 6e 63  6c 75 64 65 20 3c 73 74  |.h>.#include <st|
00000060  64 69 6f 2e 68 3e 0a 23  69 6e 63 6c 75 64 65 20  |dio.h>.#include |
00000070  3c 73 74 64 6c 69 62 2e  68 3e 0a 0a 23 64 65 66  |<stdlib.h>..#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.|

Revision as of 06:07, 16 December 2010

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.

Here is an example in C how a one time pad program might look like:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

#define PLAIN 0
#define PAD 1
 
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;
}


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...<V.|
00000010  27 bd 65 18 37 1a c2 86  c8 d5 c6 31 ef ad 4d c2  |'.e.7......1..M.|
00000020  30 91 68 c0 ab 1a 74 77  5a 90 66 7f 4f 11 ea 05  |0.h...twZ.f.O...|
00000030  d4 99 24 d9 15 86 59 64  e0 6f 7c 9d 4a 36 ee 2a  |..$...Yd.o|.J6.*|
00000040  04 52 38 3e 23 b1 73 e8  53 57 3c 14 11 5e ba df  |.R8>#.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 <sys/ty|
00000010  70 65 73 2e 68 3e 0a 23  69 6e 63 6c 75 64 65 20  |pes.h>.#include |
00000020  3c 73 79 73 2f 73 74 61  74 2e 68 3e 0a 23 69 6e  |<sys/stat.h>.#in|
00000030  63 6c 75 64 65 20 3c 66  63 6e 74 6c 2e 68 3e 0a  |clude <fcntl.h>.|
00000040  23 69 6e 63 6c 75 64 65  20 3c 75 6e 69 73 74 64  |#include <unistd|
00000050  2e 68 3e 0a 23 69 6e 63  6c 75 64 65 20 3c 73 74  |.h>.#include <st|
00000060  64 69 6f 2e 68 3e 0a 23  69 6e 63 6c 75 64 65 20  |dio.h>.#include |
00000070  3c 73 74 64 6c 69 62 2e  68 3e 0a 0a 23 64 65 66  |<stdlib.h>..#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.|